react-navigation - 从子组件导航

kcu*_*cx2 9 react-native react-navigation

我有一个排行榜,它调用一个组件并将数据传递给它,如下所示:

   _renderItem =({item}) => (
    <childComponent
        key={item._id}
        id={item._id}
        name={item.name}
    />
   );
Run Code Online (Sandbox Code Playgroud)

在childComponent中我尝试这样做:

    <TouchableOpacity onPress={() => this.props.navigation.navigate("Profile", { id: this.props.id})} >
       <View>
         <Right>
              {arrowIcon}
         </Right>
       </View>
    </TouchableOpacity>
Run Code Online (Sandbox Code Playgroud)

我希望它会转到配置文件页面,并根据传递给它的id获取正确的数据.问题是,当我单击箭头转到配置文件页面时,我得到错误无法读取属性'未定义的导航.我已经将排行榜和childComponent放在我的HomeDrawerrRoutes.js和MainStackRouter.js中.任何帮助都会很棒,谢谢.

Ada*_*rsh 25

有一个简单的解决方案,

withNavigation.它是一个更高阶的组件,它将导航道具传递给一个包装好的组件.

示例子组件

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);
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请访问:https://reactnavigation.org/docs/en/connecting-navigation-prop.html

  • 这是真正的解决方案,而不是上面的解决方案. (3认同)

tty*_*yip 8

useNavigation钩子在 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();
      }}
    />
  );
}
Run Code Online (Sandbox Code Playgroud)

文档: https: //reactnavigation.org/docs/use-navigation


Wai*_*age 6

这是一个3页的示例,演示如何将navigate函数传递给子组件以及如何自定义从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" }
    },
});
Run Code Online (Sandbox Code Playgroud)


小智 5

出于某种原因,如果您不想使用 withNavigation,以下解决方案也适用。您只需要将导航作为道具传递给您的子组件。

例如:


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

在子组件中:

const ChildComponent = (props) => {
   return (
       <View>
         <TouchableOpacity 
           onPress={() => props.navigation.navigate('Wherever you want to navigate')}      
          />
       </View>
      );
    };
export default ChildComponent;
Run Code Online (Sandbox Code Playgroud)