【问题标题】:React Navigation - this.props.navigation.state is undefinedReact Navigation - this.props.navigation.state 未定义
【发布时间】:2020-04-16 00:44:44
【问题描述】:

使用: "@react-navigation/drawer": "^5.4.0", "@react-navigation/native": "^5.1.4",

App.js:

    import { createDrawerNavigator } from '@react-navigation/drawer';
    ...
    const Drawer = createDrawerNavigator();
    const DrawerRender = () => {
    return (
      <NavigationContainer>
        <Drawer.Navigator drawerContent={props => <DrawerContent {...props} />}>
          <Drawer.Screen name="Home" component={Home} />
          <Drawer.Screen name="Screen1" component={Screen1} options={{unmountOnBlur: true, gestureEnabled: false}} />
          <Drawer.Screen name="Screen2" component={Screen2} options={{unmountOnBlur: true, gestureEnabled: false}} />
        </Drawer.Navigator>
      </NavigationContainer>
    );}

从屏幕 1:

    navigateToPage = page_id => {
      this.closeCommentScreen();
      this.reopenModal = true;
      this.props.navigation.navigate('Screen2', {'page_id': 'testing'});
    }

在屏幕 2 上:

    export default class Screen2 extends React.Component {

    constructor(props) {
       super(props);
    }
    componentDidMount() {
       let id = this.props.navigation.state.params.page_id
       ...

我尝试打印“this.props.navigation”,它总是返回空字典 {},因此参数未定义。

我做错了什么?

【问题讨论】:

  • 看起来像this
  • 我的 this.props.navigation 是空的。 state.params 将始终为空
  • 两个屏幕都不能用?
  • navigation.navigate 可以正常工作。当我尝试从 Screen2 访问参数时,它会出错

标签: react-native react-native-android react-navigation react-native-ios


【解决方案1】:

我是this.props.route.params

查看升级指南https://reactnavigation.org/docs/upgrading-from-4.x#separate-route-prop

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    相关资源
    最近更新 更多