Passing parameter navigation from stack navigation to child component

Asked

Viewed 429 times

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?

  • 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

  • you gave a console.log(props) and have not?

  • That I have not tried, I usually unstructure the props, I will try this to see without unstructuring

  • <Admin navigation={navigation} /> is what I’m talking about will need to pass this props, could be removed and tested as well navigation={navigation} since the components are involved by navigation

  • 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

  • It’s hard because I don’t understand the scenario

  • 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

  • This component is inside the Navigation?

  • The Card component is separated into another file

  • Separated all right, I want to know if he is inside the Child (children) where the Greatest Father is Navigation?

  • 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

  • The Navigator is inside the main or outside?

  • Navigator is passed automatically by Stack Navigator pro main but I need to use it within the q component is within admin

Show 9 more comments

2 answers

2


Hook useNavigation

An alternative, besides the one given by @Lucassouza, is to use the hook useNavigation. Like the documentation itself says, it is most useful when you do not want to pass the prop navigation for a child component, or when you have a very nested component tree, where you would have to stay passing the prop for each component of the tree, until you reach the point where you want it. For example:

<Main>
  <Header>
    <NavList>
       {/* aqui usa a prop `navigation` */}
       <NavLink />
    </NavList>
  </Header>
</Main>

To use the hook useNavigation, is the same as the others Hooks:

import React from 'react';
import { Button } from 'react-native';
import { useNavigation } from '@react-navigation/native';

export default function MyButton() {
    const navigation = useNavigation();

    return (
        <Button
            title="Click me!"
            onPress={() => navigation.navigate('MyRoute')}
        />
    );
}

You import it and make the function call.

The warning message

Another thing to note is that the error message:

ERROR Warning: Cannot update a component from inside the function body of a different component.

Appears when you change a state of a component in rendering, from another component, as per can be seen in the React documentation.

So depending on how you’re doing, or maybe how the react-navigation works, the state of one component is being changed by another, in an inappropriate way.

  • Perfect guy, I had no idea I had a hook for that separate, and look who I researched huh, but it seems not enough, thank you very much

1

An alternative for your case would be to centralize in your Main component the navigation functions and descend these functions via direct link, example:

export default Main = ({navigation}) => {
    const navegaAdmin = navigation => navigation.navigate('nome-da-rota');
    
    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 navegar={navegaAdmin} />}
       </ScrollView>
     </>
   )
 }

And your Admin component on the button would look something like this:

<TouchableOpacity onPress={navegar} />

If you do not want to destructing the props in your Admin component then the navigation button would look like this:

<TouchableOpacity onPress={props.navegar} />
  • Dear thank you very much, your reply worked mt well, I was doing half right then right, but had q pass the function all right ja with the name of the route.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.