0
I’m new to React On and I’m building an app where I need to rethink the same code several times with only a few differences, so I’m creating components for this, but in one of the components there is a button and when pressed it navigates to another route, usually when I need to do this the stack navigation always passes as props the navigation property and we use it as follows navigation.navigate('nome-da-rota')
, however I need to use this property inside the child component, but every time I try I get the error ERROR Warning: Cannot update a component from inside the function body of a different component.
My component is in another file
My code:
export default function Main({navigation}) {
return (
<>
<View style={styles.header}>
<TouchableOpacity style={styles.touch} onPress={logout}>
<Image source={Logout} style={styles.logoutImage}></Image>
<Text style={styles.outText}>Sair</Text>
</TouchableOpacity>
</View>
<ScrollView>
{admin && <Admin navigation={navigation} />}
</ScrollView>
</>
)
}
The child component should already have the
navigation
, already tested not pass and only recover?– novic
Already, he n has, I even tried to create a Stack just for him to receive this parameter, but it seems that this parameter is only passed when the route is called, n rendered
– Geovane
you gave a console.log(props) and have not?
– novic
That I have not tried, I usually unstructure the props, I will try this to see without unstructuring
– Geovane
<Admin navigation={navigation} />
is what I’m talking about will need to pass thisprops
, could be removed and tested as wellnavigation={navigation}
since the components are involved by navigation– novic
There is yes understood Then I tried to take it out, and recover it in the component he from Undefined pq n can find the props q’m using, but it n works pq is in an external file, I will try to define my component inside the parent component, n will get organized but if it works great kkkk
– Geovane
It’s hard because I don’t understand the scenario
– novic
Let me try to summarize, I’m creating an app where there will be levels of access, and in the main is where it checks if the access and redirects to the corresponding component, only q this component te admin by ex inside it there is a panel where depending on the button the user click it will be redirected to another page in the application, then I need inside my admin component would have another Card component where it would be called several times with different routes, so to n repeat it created a separate but this card needs to navigate to another route, here’s the problem
– Geovane
This component is inside the Navigation?
– novic
The Card component is separated into another file
– Geovane
Separated all right, I want to know if he is inside the Child (children) where the Greatest Father is Navigation?
– novic
Yes it is in the case inside my main component q is the parent with the Navigator step pro admin q is the son and inside the admin step pro TMB Card
– Geovane
The Navigator is inside the main or outside?
– novic
Navigator is passed automatically by Stack Navigator pro main but I need to use it within the q component is within admin
– Geovane