Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
React Native - Same Function call in child component but different result

maybe this question has been asked before, but unfortunately I was unable to find the answer.

I have got a screen called AccountManagement, where I can change the users data and in the end there is a button which updates the data.

const AccountManagement = (props) => {
    const {userData} = useContext(AuthContext)
    const [name, setName] = useState(userData.name)
    //other data...
    const update = () => {
        userData.name = name
        // set other data
        updateProfile(userData)
        navigation.navigate('Profile')
    }
}

So far, so good everything works fine. But if I call the same function (update()) in an other component via props, the changed state of name for example is ignored. Below, there is the return call of the same screen AccountManagement

 return(
    <SafeAreaView style={styles.container}>
        <HeaderBar updateInfo={update}/>
        <TextInput 
            style={styles.input}
            value={name}
            onChangeText={setName}
            placeholder=""/>
    <TouchableOpacity style={styles.changeContainer} 
                      onPress={update}>
        <Text style={styles.change}>Update Info</Text>
    </TouchableOpacity>
)

In the component HeaderBar, I have got a TouchableOpacity, which should do the exact thing as the TouchableOpacity in AccountManagement, call update(), which it does, but the changed name is never used when I press the TouchableOpacity in the HeaderBar component

const HeaderBar = (props) => {
  return(
    <View>
        <TouchableOpacity onPress={props.updateInfo} style={styles.rightContainer}>
            <Text style={styles.rightText}>Update Info</Text>
        </TouchableOpacity>
    </View>
  }
)

Even if I try to print with console.log I cannot see the change.

My question now is:

Why is the changed name ignored, when I press update from the HeaderBar component?

How can I fix this issue?

Thank you in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

because your function update is not invoked in AccountManagement neither in HeaderBar

 <HeaderBar updateInfo={() => update()}/>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda