【问题标题】:React Navigation - cannot read property 'navigate' of undefinedReact Navigation - 无法读取未定义的属性“导航”
【发布时间】:2017-03-14 18:30:39
【问题描述】:

我一直在尝试使用 react navigation 启动并运行,但是当我尝试将导航项移动到它们自己的组件中时遇到了问题。

HomeScreen.js

import React, { Component } from 'react';

import {
  StyleSheet,
  View,
  Text
} from 'react-native';

import NavButton from '../components/NavButton'

class HomeScreen extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text>
        Hello World
        </Text>

        <NavButton
        navigate={this.props.navigator}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  }
});


export default HomeScreen;

然后在 NavButton 组件中,我尝试导航到新屏幕。

 class NavButton extends Component {
  render() {
    return (
      <View>
        <Button
        title="Go to About"
        onPress={() => this.props.navigator.navigate('About')}
        />
      </View>
    );
  }
}
export default NavButton;

但我不断收到错误“无法读取未定义的属性‘导航’。

这也是我的 Router.js 文件。

import React from 'react';
import {StackNavigator} from 'react-navigation';

import HomeScreen from '../screens/HomeScreen'
import AboutScreen from '../screens/AboutScreen'


export const AppNavigator = StackNavigator({
  Home: {
    screen: HomeScreen
  },
  About: {
    screen: AboutScreen
  }
})

【问题讨论】:

    标签: javascript react-native react-navigation


    【解决方案1】:

    如果您将 navigate={this.props.navigator} 重命名为 navigator={this.props.navigation},它应该可以工作,因为在 NavButton 中您正在调用 this.props.navigator.navigate

    【讨论】:

    • navButton 上的 onPress 函数仍然报错。
    • 哦,我认为它应该是 this.props.navigation 而不是 this.props.navigator
    • 想通了!正确使用的词是 navigation={this.props.navigation}。
    • 如何使用navigator={this.props.navigation}?
    【解决方案2】:

    非屏幕组件的普通组件默认不会接收到导航属性。

    要解决此异常,您可以在从屏幕渲染时将导航道具传递给 NavButton,如下所示:&lt;NavButton navigation={this.props.navigation} /&gt;

    或者,我们可以使用withNavigation函数自动提供导航道具

    
    import { withNavigation } from 'react-navigation';
    
    class NavButton extends React.Component {
      render() {
        return (
          <Button
            title="Back"
            onPress={() => {
              this.props.navigation.goBack();
            }}
          />
        );
      }
    }
    
    // withNavigation returns a component that wraps NavButton and passes in the
    // navigation prop
    export default withNavigation(NavButton);
    
    

    参考:https://reactnavigation.org/docs/en/connecting-navigation-prop.html

    【讨论】:

      【解决方案3】:

      确保你正在编译 ES6

      如果不是简单地使用 this.props.navigation 或者 this.props.navigation.navigate 你需要什么

      【讨论】:

        【解决方案4】:
        import * as React from 'react';
        import { Button } from 'react-native';
        import { useNavigation } from '@react-navigation/native';
        
        function GoToButton() {
          const navigation = useNavigation();
        
          return (
            <Button
              title='Screen Name'
              onPress={() => navigation.navigate(screenName)}
            />
          );
        }
        

        您可以将 @react-navigation/native 中的 useNavigation 与 React Navigation v 5.x 一起使用。 更多详情请关注documentation

        【讨论】:

          猜你喜欢
          • 2018-10-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-06-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多