【问题标题】:How to use react navigation in a const?如何在 const 中使用反应导航?
【发布时间】:2019-02-08 15:13:30
【问题描述】:

我使用 const 来显示组件。现在,当我对 const 中的按钮使用反应导航时,我看到了这个错误:undefined is not an object (evalating '_this.props.navigation.navigate')

我尝试将 navigation={this.props.navigation} 添加到按钮以允许导航,但没有成功。

const WomenTab = () => (
    <View>
      <Button onPress={() => {
                        this.props.navigation.dispatch(StackActions.reset({
                          index: 0,
                          actions: [
                            NavigationActions.navigate({ routeName: 'Wallet' })
                          ],
                        }))
                      }}>
          <Text>Click</Text>
      </Button>
    <View>
);

库链接:http://github.com/react-native-community/react-native-tab-view

【问题讨论】:

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


    【解决方案1】:

    这称为functional component,通常称为无状态功能组件。

    主要区别之一是 SFC 不会自动接收 props,而是必须作为参数传递。因此,与其说this.props,不如使用这种模式:

    const WomenTab = (props) => ( // <-- add props as an argument
      <View>
        <Button onPress={() => {
          props.navigation.dispatch(StackActions.reset({
            index: 0,
            actions: [ NavigationActions.navigate({ routeName: 'Wallet' }) ],
          }))
        }}>
          <Text>Click</Text>
        </Button>
      <View>
    );
    

    由于导航道具会自动传递给导航器的子节点,因此您无需执行任何其他操作。如果你想传递其他道具,你照常这样做:

    <WomenTab myProp={value} />
    

    另一种常见的模式是 destructure 传递给 SFC 的 props,如下所示:

    const WomenTab = ( {navigation} ) => ( // <-- you can pick the props you want via destructuring
      <View>
        <Button onPress={() => {
          navigation.dispatch(StackActions.reset({
            index: 0,
            actions: [ NavigationActions.navigate({ routeName: 'Wallet' }) ],
          }))
        }}>
          <Text>Click</Text>
        </Button>
      <View>
    );
    

    希望对您有所帮助,祝您好运!

    【讨论】:

    • 不错的答案。我喜欢你的详细解释,解构是一个很好的补充。
    • 谢谢。当我在普通 const 中使用时,这工作得很好。但是现在我在这个库中有另一个 const,并且不为此工作。链接:github.com/react-native-community/react-native-tab-view
    【解决方案2】:

    您需要将props 传递给const,类似这样

    const WomenTab = (props) => (
        <View>
          <Button onPress={() => {
                            props.navigation.dispatch(StackActions.reset({
                              index: 0,
                              actions: [
                                NavigationActions.navigate({ routeName: 'Wallet' })
                              ],
                            }))
                          }}>
              <Text>Click</Text>
          </Button>
        <View>
    );
    

    然后,当您使用 const 时,您会传递所需的道具。

    <WomenTab navigation={this.props.navigation} />
    

    【讨论】:

    【解决方案3】:

    基本上,您的道具不会从父组件传递到子组件。确保您已在 createStackNavigator 函数中定义了 WomenTab 组件。还要在你的函数组件中传递道具。

    const WomenTab = (props) => (
    <View>
      <Button onPress={() => {
                        this.props.navigation.dispatch(StackActions.reset({
                          index: 0,
                          actions: [
                            NavigationActions.navigate({ routeName: 'Wallet' })
                          ],
                        }))
                      }}>
          <Text>Click</Text>
      </Button>
    <View>
    

    );

    【讨论】:

    • 这不起作用,因为您使用的是this.props.navigation,没有this,您需要删除this.才能使其工作。
    • @Andrew 谢谢你的提及。我完全忘了删除 this 关键字。
    猜你喜欢
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多