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

231
Visualizações
Unable to store route params in useState (react native navigation)

I am trying to implement this navigation pattern when you click a button then it takes you to a new set of screens then comes back right at the start, something like this:

parentScreen => child 1 => child 2 => parentScreen(again).

However, I am having trouble storing route params coming from "child 2" in "parentScreen" using react native navigation route object

here is the code of the parentScreen:

  useEffect(() => {
    axios
      .get(`${baseUrl}/utilities/Entretien?id=1`)
      .then((response) => {
        setDataFromAPI(response.data)
      })
      .catch((error) => {
        console.log(error)
      })
  }, [])

  // storing route.params.params inside recap state 
  
  //route.params example: 
  /* Object {
  "merge": true,
  "name": "child 2",
  "params": Object {
    "id": 20,
    "name": "create",
    "parent": "wheel",
  },
} */
  useEffect(() => {
    console.log('this is route: ', route.params)
    const newRecap = route.params?.params
    if (route.params?.params !== undefined)
      setRecap((prev) => [...prev, newRecap])
    console.log('recap', recap)
  }, [route.params])

this is how a pass the params to parent screen

let { id, baseUrl, parent } = route.params
  const [selectedId, setSelectedId] = React.useState(null)
  const [dataFromAPI, setDataFromAPI] = React.useState([])
  React.useEffect(() => {
    axios
      .get(`${baseUrl}/utilities/Entretien?id=${id}`)
      .then((response) => {
        setDataFromAPI(response.data)
      })
      .catch((error) => {
        console.log(error)
      })
  }, [])
const renderItem = ({ item }) => {
    return (
      <Item
        item={item}
        onPress={() => {
          navigation.navigate({
            name: 'Entretien',
            params: {
              id: item.id,
              name: item.value,
              parent,
            },
            merge: true,
          })
        }}
      />
    )
  }
  return (
    <View>
      <FlatList
        data={dataFromAPI}
        renderItem={renderItem}
        keyExtractor={(item) => item.id}
        extraData={selectedId}
      />
    </View>
  )

the problem is when I log the recap state I get an empty array even after navigating through the child screens, it is until I re-render (save the file once again) that the recap state matches the route.params, and if I re-render again, the recap array will contain two of route.params object.

Any ideas on how I can fix this ? Thank you (for any explanation please leave a comment 😁) (react 17.0.2, react-navigation 6, expo 45, react-native 0.68.2)

about 4 years ago · Juan Pablo Isaza
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