Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
React Native - Header & Accessing State

I'm trying to have a header button in my screen that access the screen state and validates if a post is ready to send to the DB (ie. its not an empty string and if theres an image post the image too).

When i create a full post (image and text) and try and fire the post function in the screen, it works (see the 'test' button on the screen)

When i create a full post (image and text) and try and fire the post function from the header, it does not grab the state properly.

What is happening here? How do i resolve this?

Screen State & Header

function PostScreen(props) {

const [newPost, setNewPost] = useState('')
const [isLoading, setIsLoading] = useState(false)
const [mediaAsset, setMediaAsset] = useState('')

 React.useLayoutEffect(() => {
        props.navigation.setOptions({
            headerRight: () => {
                return (
                    isLoading ? <ActivityIndicator color='#4292CE' size='small' style={{ marginRight: 10 }} /> :
                        <TouchableOpacity style={styles.postButton} onPress={() => postFunction()}>
                            <Text style={styles.postText}>Post</Text>
                        </TouchableOpacity>)
            },
            title: null
        });
    }, [isLoading, newPost]);

Post Function

let postFunction = async () => {
      setIsLoading(true)

      if (newPost === '') {
          console.log(newPost) // this comes up as nothing when the postFunction is executed from the header
        }
        
        else if (mediaAsset === '') {
            //! post only the text
        }

        else if (mediaAsset != ''){
            //! post the text and the image
        }

      setIsLoading(false)
}

Test Environment

enter image description here

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda