【问题标题】:having problem with react-native navigation | undefined is not an object (evaluating '_this.props.navigation')react-native 导航有问题| undefined 不是对象(评估 '_this.props.navigation')
【发布时间】:2019-06-13 19:14:48
【问题描述】:

您好,我正在开发一个新的 react-native 应用程序,但我在从组件导航到屏幕时遇到了一些问题。 这是零食代码的链接:https://snack.expo.io/@mimonoux/my-app-navigation-test

我已经试过了 <ButtonCarte onPress={() => this.props.navigation.navigate('Carte') } />。 但它没有用。请如果有人可以帮助我,请检查零食链接并深入了解我为我的实际问题编写的简单代码

【问题讨论】:

  • 在你的 ButtonCarte 组件中,你可以试试这个:<TouchableOpacity onPress={() => onPress()} style={buttonStyle}>
  • 谢谢你的回答,是的,我做到了,它就在那里,你有没有看过我在世博小吃上的迷你应用程序,如果没有,请看看我在努力解决什么问题这样你就可以帮我涂黄油了,谢谢
  • 可能你没听懂我的意思。是的,我通过链接看到了您的代码。可能在您的 ButtonCarte 组件中,请尝试将 <TouchableOpacity onPress={onPress} style={buttonStyle}> 替换为 <TouchableOpacity onPress={() => onPress()} style={buttonStyle}>
  • 是的,是的,非常感谢你,我的朋友,我确实非常了解你,这正是我没有做到的,但仍然没有为我工作,你在我的博览会小吃代码上试过了吗?为你工作??

标签: react-native react-redux navigation react-navigation react-native-navigation


【解决方案1】:

我现在看到了你的问题。使用 react-navigationnavigation props 存在于组件中:要么组件配置在您在 App.js 中定义的路由配置对象中,要么使用 withNavigation HOC (https://reactnavigation.org/docs/en/with-navigation.html )。

现在在 Medicine_listDetail 组件中 this.props.navigation 不存在,因为 Medicine_listDetail 并没有出现在您的路线中,而且 props 对象不应被 this.props 在功能组件中读取。您可以采用以下两种方式之一:

const Medicine_listDetail = ({medicine, navigation}) => {
    // i'm passing navigation props comme from parent component that have
    // navigation object
    // ...
}

// OR you can do

const Medicine_listDetail = (props) => {
    const { medicine, navigation } = props;
    // i'm passing navigation props comme from parent component that have
    // navigation object
    // ...
}

因此,以下是对我有用的解决方案的尝试。

  • Medicine_listDetail 组件:我传递的导航道具来自 具有导航对象的父组件
...
const Medicine_listDetail = ({medicine, navigation}) => {

    const {title, coordinate} = medicine;
    const {
        headerContentStyle,
        headerTextStyle,
        cityTextStyle,
        addTextStyle,
        infoContainerStyle,
        buttonsContainerStyle,
        specialityTextStyle,
        buttonStyle,
        textStyle
        } = styles


    return (
    <View>

        <View style={headerContentStyle}>
            <Text style={headerTextStyle}>{title}</Text>
        </View>

        <View style={buttonsContainerStyle}>
          <ButtonCarte  onPress={() => navigation.navigate('Carte') }>
          </ButtonCarte>
        </View>
    </View>
    );
};
...
  • ButtonCarte 组件
const ButtonCarte = ({onPress, children}) => {
  const {buttonStyle, textStyle} = styles;

  return (
    <TouchableOpacity onPress={() => onPress()} style={buttonStyle}>
        <Ionicons name={'ios-pin'} size={20} color="white" />
        <Text style={textStyle}>
          Voir La Carte
        </Text>
    </TouchableOpacity>
  ); 
};
  • Medicin 组件:在 all_medicine() 函数中,我在 Medicine_listDetail 组件的 props 中传递导航对象。所以这就是诀窍。
export default class Medicin extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      list_allMedicine: data_allMedicine,
      selectedIndex: 0,
    };
    this.updateIndex = this.updateIndex.bind(this);
  }

  updateIndex(selectedIndex) {
    this.setState({ selectedIndex });
  }

  all_medicine() {
    const { navigation } = this.props;
    return this.state.list_allMedicine.map(medicine => (
      <Medicine_listDetail key={medicine.title} medicine={medicine} navigation={navigation} />
    ));
  }

  render() {
    const buttons = ['Tout', '...', '...', '...'];
    const { selectedIndex } = this.state;
    return (
      <View style={{ flex: 1}}>
        <View
          style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <ButtonGroup
            onPress={this.updateIndex}
            selectedIndex={selectedIndex}
            buttons={buttons}
            containerStyle={{ borderRadius:8 }}

          />
        </View>
        <Divider
          style={{
            backgroundColor: 'lightgrey',
            marginHorizontal: 5,
            height: 2,
          }}
        />
        <View style={{ flex: 5 }}>
          {this.state.selectedIndex == 0 ? (
            <ScrollView>{this.all_medicine()}</ScrollView>
          ) : (
            <Text>test</Text>
          )}
        </View>
      </View>
    );
  }
}
  • 至少在 App.js 中,由于您的 RootStack 堆栈,我将 carte 选项卡的名称从 Cart 更改为 Carte。李>
export default createAppContainer(
  createBottomTabNavigator(
    {
      Home: {
        screen: Home,
        navigationOptions: {
          tabBarLabel: 'Home',
          tabBarIcon: ({ tintColor }) => (
            <Ionicons name={'ios-home'} size={25} color={tintColor} />
          ),
        },
      },
      Medicin: {
        screen: Medicin,
        navigationOptions: {
          tabBarLabel: 'Medicin',
          tabBarIcon: ({ tintColor }) => (
            <Image
              source={require('./assets/images/Dashboard/drawable-xhdpi/doctor_heart.png')}
              style={{ width: 25, height: 20, tintColor: tintColor }}
            />
          ),
        },
      },
      Carte: {
        screen: Carte,
        navigationOptions: {
          tabBarLabel: 'Carte',
          tabBarIcon: ({ tintColor }) => (
            <Ionicons name={'ios-map'} size={25} color={tintColor} />
          ),
        },
      },
    },
    {
      tabBarOptions: {
        activeTintColor: 'black',
        inactiveTintColor: 'gray',
      },
    }
  )
);

我对此进行了测试,它对我有用。

【讨论】:

  • 非常感谢你,兄弟,你救了我的命,现在一切正常:)
【解决方案2】:

尝试添加这个:

import { NavigationEvents, NavigationActions } from 'react-navigation';

以下是参考以下 cmets 的 props 中可用内容的屏幕截图:

这是我在 cmets 中提到的屏幕截图。你可以看到我在哪里添加了一个console.log。它在控制台中显示,虽然导航在 this.props 中,但导航中的操作是空的。我认为这就是问题的根源。如果你像我所做的那样放置更多的 console.logs,你会看到它在项目中的哪个位置丢失了这些信息。

【讨论】:

  • 感谢您的回答,我尝试将此行添加到每个页面中,但仍然无效。你有没有看我在世博小吃上的应用,如果没有,请看一下以了解我正在努力解决的问题
  • 对不起,如果不是这样,那么我不确定出了什么问题。我看了你的代码。我习惯于反应导航,但不是专门用于标签导航器。也许这是特定的东西?它是否告诉您 this.props.navigation 仅在您想使用它的地方或任何地方未定义?也许可以尝试将 console.logs 放在所有地方,看看它是否从未定义过,或者是否有但没有传递给每个组件。
  • 我真的不知道标签导航器是否存在问题,但我已经尝试了所有我能找到或想到的解决方案。是的,它告诉我this.props.navigation is undefined 只有当我将它添加到ButtonCard 组件中时,它才能正常工作。抱歉,我真的不习惯使用console.logs 来定义错误,所以请你再看看我的代码并为我做,我真的需要尽快完成这个应用程序,我真的很感激跨度>
  • 好的,例如,在药物清单的第 32 行,您可以尝试 console.log(this.props)}> 并查看导航是否在其中。而且我还建议将 console.log(this.props) 放在您渲染的任何地方,例如 carte.js 的第 35 行、home 的 31 行和医学的 41 行,就像 medicin.js 中的这种更改:render() { 常量 { 导航 } = this.props;常量按钮 = ['Tout', '...', '...', '...'];常量 { selectedIndex } = this.state;控制台.log(this.props);返回(
  • 我将编辑我的原始答案以添加屏幕截图的图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-14
  • 2022-01-23
  • 2017-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多