【问题标题】:Unable to access route params (functional component)无法访问路由参数(功能组件)
【发布时间】:2021-03-08 01:30:13
【问题描述】:

我正在尝试将用户名从注册 1 传递给注册 2:

handleSignUp = () => {
      console.log(this.state.username) <------------- This logs correctly 

      try {
        this.props.navigation.navigate('Signup2', {
          username: this.state.username, <------------- Passing here
        });
      } catch (error) {
        console.log(error);
      }
    }

这是来自 Signup2 的代码:

const Signup2 = (props, route, navigation) => {

  const { username } = route.params; <----------- Trying to access it here

我收到以下错误:

undefined is not an object (evaluating 'route.params.username')

我也试过这个:

const { username } = props.route.params

两者都不起作用。

如何访问路由参数?

【问题讨论】:

    标签: javascript react-native react-navigation


    【解决方案1】:

    如果您想按预期访问它们,我认为您需要解构您的路线和导航属性。

    你可以去:

    const Signup2 = ({route, navigation}) => {
       const { username } = route.params
    }
    

    或许:

    const Signup2 = (props) => {
       const { username } = props.route.params
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 2019-06-29
      • 2020-03-31
      • 1970-01-01
      • 2018-07-07
      相关资源
      最近更新 更多