【问题标题】:React Native TabNavigator, Cannot read property 'navigate' of undefinedReact Native TabNavigator,无法读取未定义的属性“导航”
【发布时间】:2017-12-20 19:08:32
【问题描述】:

我刚刚设置了一个工作正常的 React Navigation TabNavigator,但我在视图之间的转换方面遇到了问题。

我在单击按钮后尝试将用户重定向到另一个页面时收到以下错误消息。

无法读取未定义的属性“导航”

我对如何完成这项工作感到非常困惑。

这里是导航器:

import React from 'react';
import { TabNavigator, StackNavigator } from 'react-navigation';
import ElemList from './src/components/ElemList';
import ElemShow from './src/components/ElemShow';

const RootTabs = TabNavigator({
  Home: {
    screen: ElemList,
    navigationOptions: {
      tabBarLabel: 'Home',
      tabBarIcon: ({ tintColor, focused }) => (
        <Ionicons
          name={focused ? 'ios-home' : 'ios-home-outline'}
          size={26}
          style={{ color: tintColor }}
        />
      ),
    },
    main: {
      screen: StackNavigator({
        show: { screen: ElemShow },
      })
    }
  }
});

这是我的代码:

onRowPress(data) {
this.props.navigation.navigate('ElemShow', {id: data})
};

render() {

const { key, elem } = this.props;

 return (
  <TouchableWithoutFeedback onPress={() => this.onRowPress(elem)}>
    <View style={styles.viewStyle} key={key}>
      <Text>Here is the info</Text>
    </View>
  </TouchableWithoutFeedback>
 )

}

【问题讨论】:

  • 你在哪里定义 elem 传递给 onPress 中的函数?
  • @Max 在 this.props 中,抱歉忘了提。但错误似乎来自于未定义导航的事实。

标签: javascript reactjs react-native react-navigation react-native-navigation


【解决方案1】:

问题出在这一行

this.props.navigation

应该是

this.props.navigator

另外我不知道navigate 是屏幕API 的一部分;通常你会使用pushresetTo

【讨论】:

  • 我仍然不确定。
  • @Hugo 你是在一个组件中调用onRowPress吗?
  • 是的,我是。这个组件是一个 ListItem,我通过 map 函数将它传递给我的 List。
  • 您能否发布完整的组件代码,包括您根据我的回答所做的修复?
  • 很抱歉我要关闭这个问题我切换到 React Native Router Flux,这个 React Navigation 有太多的错误。
猜你喜欢
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多