Pass navigation.navigate to Child Component

后端 未结 6 1492
轻奢々
轻奢々 2021-02-09 16:32

Building an app using react-navigation. I have a parent component that pulls from firebase and renders data in a listview. The listview render component \'ListName\' has an onRo

相关标签:
6条回答
  • 2021-02-09 16:48

    You can also pass the navigation prop from the parent to the child directly by doing this:

    renderRow(user) {
        return <ListName name={user[0]} navigation={this.props.navigation}/>;
    }
    

    This will allow you to use react-navigation's navigate method in ListName.

    Info also from this page: https://reactnavigation.org/docs/en/connecting-navigation-prop.html

    0 讨论(0)
  • 2021-02-09 16:50
    import {withNavigation} from 'react-navigation';
    
    class Parent extends Component{
      <Child {...this.props}/>
    }
    
    class Child extends Component {
      <TouchableOpacity onPress={()=>this.props.navigation.navigate("Screen")}>
        <V><T>
         {PRESS ME}
        </V></T>
      </TouchableOpacity>
    }
    
    export default withNavigation(Parent);
    
    0 讨论(0)
  • 2021-02-09 16:52

    One way is to have a onPress handler in your ProvideScreen component that is passed down to your list item component and thus in ProvideScreen's onRowPress function, you will have access to this.props.navigation.

    In ProvideScreen:

    class ProvideScreen extends Component {
      ...
      onRowPress(someRowData) {
        this.props.navigation.navigate('Feedback', { someRowData });
      }
      ...
    
      renderRow(user) {
        return <ListName name={user[0]} onRowPress={this.onRowPress} />;
      }
    }
    

    In ListName:

    class ListName extends Component {
      ...
      onRowPress() {
        this.props.onRowPress(someData);
      }
      ...
    }
    

    Of course, don't forget about binding the functions to this scope when you are using event handlers. e.g. in constructor:

    this.onRowPress = this.onRowPress.bind(this);
    

    An alternative define onRowPress with an arrow functions:

    onRowPress = () => {
     this.props.navigation.navigate...
    }
    
    0 讨论(0)
  • 2021-02-09 16:57

    i just add this prop from parent and it worked:

    <AppFooter navigation={this.props.navigation}  />
    
    0 讨论(0)
  • 2021-02-09 16:59

    Pass all props like

    Form

    {...this.props}
    

    here <Form> is my child components

    other wise.

    import { withNavigation } from 'react-navigation';
    

    Then in the end

    export default withNavigation(<ChildComponentName>);
    
    0 讨论(0)
  • 2021-02-09 17:14

    Base on React Navigation: https://reactnavigation.org/docs/en/connecting-navigation-prop.html

    You just need to import withNavigation into child component only.

    import { withNavigation } from 'react-navigation';
    

    then export:

    export default withNavigation(<YourChildComponent>);
    
    0 讨论(0)
提交回复
热议问题