react-navigation - navigating from child component

后端 未结 4 678
面向向阳花
面向向阳花 2020-12-16 01:12

I have a leaderboard which calls a component and passes it data to it like so:

   _renderItem =({item}) => (
    

        
相关标签:
4条回答
  • 2020-12-16 01:46

    There is an easy Solution for this,

    use withNavigation . it's a higher order component which passes the navigation prop into a wrapped Component.

    example child component

    import React from 'react';
    import { Button } from 'react-native';
    import { withNavigation } from 'react-navigation';
    
    class ChildComponent extends React.Component {
      render() {
        <View
          onPress = {()=> this.props.navigation.navigate('NewComponent')}>
         ... logic
        </View>
    
      }
    }
    
    // withNavigation returns a component that wraps ChildComponent and passes in the
    // navigation prop
    export default withNavigation(ChildComponent);
    

    for more details : https://reactnavigation.org/docs/en/connecting-navigation-prop.html

    0 讨论(0)
  • 2020-12-16 01:46

    The useNavigation hook was introduced in v5:

    import * as React from 'react';
    import { Button } from 'react-native';
    import { useNavigation } from '@react-navigation/native';
    
    export function ChildComponent() => {
      const navigation = useNavigation();
    
      return (
        <Button
          title="Back"
          onPress={() => {
            navigation.goBack();
          }}
        />
      );
    }
    

    Docs: https://reactnavigation.org/docs/use-navigation

    0 讨论(0)
  • 2020-12-16 01:56

    For some reason if you don't want to use withNavigation, the following solution works too. You just have to pass navigation as a prop to your child component.

    For example:

    
    export default class ParentComponent extends React.Component {
     render() {
       return (
          <View>
            <ChildComponent navigation={this.props.navigation} />
          </View>
          );
        }
      }
    

    And in child component:

    const ChildComponent = (props) => {
       return (
           <View>
             <TouchableOpacity 
               onPress={() => props.navigation.navigate('Wherever you want to navigate')}      
              />
           </View>
          );
        };
    export default ChildComponent;
    
    0 讨论(0)
  • 2020-12-16 01:59

    This is a 3 page example that shows how to pass the navigate function to a child component and how to customize props send to screens from within the StackNavigator

    // subcomponent ... receives navigate from parent
    const Child = (props) => {
        return (
            <TouchableOpacity 
                onPress={() => props.navigate(props.destination) }>
                <Text>{props.text}>>></Text>
            </TouchableOpacity>
        );
    }
    // receives navigation from StackNavigator
    const PageOne = (props) => {
        return (
            <View>
                <Text>Page One</Text>
                <Child 
                    navigate={props.navigation.navigate} 
                    destination="pagetwo" text="To page 2"/>
            </View>
        )
    }
    // receives custom props AND navigate inside StackNavigator 
    const PageTwo = (props) => (
        <View>
            <Text>{props.text}</Text>
            <Child 
                navigate={props.navigation.navigate} 
                destination="pagethree" text="To page 3"/>
        </View>
    );
    // receives ONLY custom props (no nav sent) inside StackNAvigator
    const PageThree = (props) => <View><Text>{props.text}</Text></View>
    
    export default App = StackNavigator({
        pageone: { 
            screen: PageOne, navigationOptions: { title: "One" } },
        pagetwo: { 
            screen: (navigation) => <PageTwo {...navigation} text="Page Deux" />, 
            navigationOptions: { title: "Two" } 
        },
        pagethree: { 
            screen: () => <PageThree text="Page III" />, 
            navigationOptions: { title: "Three" }
        },
    });
    
    0 讨论(0)
提交回复
热议问题