首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React导航5参数传递的问题

React导航5参数传递的问题
EN

Stack Overflow用户
提问于 2020-09-18 15:58:51
回答 2查看 249关注 0票数 0

我是第一次接触react-native。我试图在成功获取API后将一些数据与导航一起传递。

代码语言:javascript
复制
 if (response.status === 200) {
    navigation.navigate(EditProfile, {
      password: password,
      address: 'Address',
      firstname: 'John',
      lastname: 'Doe',
    });
  }

在接收屏幕中,根据react导航5,我使用了"route“关键字来获取附加的参数。首先,我使用了

代码语言:javascript
复制
const {firstname} = route.params

它不起作用,所以我尝试记录params的输出。

代码语言:javascript
复制
const EditProfile = ({route}) => {
console.log(route.params);
console.log(route);
return(someJSX)

输出:

代码语言:javascript
复制
LOG      undefined
LOG      {"key": "EditProfile-eaMKo66zwdmYhxnS-uOdr", "name": "EditProfile", "params": undefined}

我被困在这里好几个小时了,帮我一下谢谢。

EN

回答 2

Stack Overflow用户

发布于 2020-09-18 16:08:29

代码语言:javascript
复制
// set navigation params
const navigation = this.props.navigation;
        navigation.navigate(EditProfile, {
            password: password,
            address: 'Address',
            firstname: 'John',
            lastname: 'Doe',
        });


 
//receive navigation objects: try this
     this.props.navigation.getParam('password')
 or 
 // Access the  otherParam via Destructuring assignment
    const { otherParam } = this.props.route.params;

https://www.positronx.io/react-native-stack-navigator-passing-getting-params-to-screen/

票数 0
EN

Stack Overflow用户

发布于 2020-11-24 10:39:36

试用hook useNavigation,react-导航版本: 5.x

代码语言:javascript
复制
// File SomeComponent.js
import { useNavigation } from '@react-navigation/core';

const SomeComponent = () => {
  const navigation = useNavigation();

  navigation.navigate('EditProfile', {
    password: password,
    address: 'Address',
    firstname: 'John',
    lastname: 'Doe',
  });
}

// File EditProfile.js
const EditProfile = ({ route }) => {
  const { password, address, firstname, lastname } = route.params;
  // do something with these values
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63951579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档