简体   繁体   English

React Navigation 5 参数传递的问题

[英]Problem with React Navigation 5 params passing

I am new to react-native.我是本机反应的新手。 I am trying to pass some data along with the navigation after successful API fetch.我试图在成功获取 API 后将一些数据与导航一起传递。

 if (response.status === 200) {
    navigation.navigate(EditProfile, {
      password: password,
      address: 'Address',
      firstname: 'John',
      lastname: 'Doe',
    });
  }

in the receiving screen, according to the react navigation 5 I used "route" keyword to get the attached params.在接收屏幕中,根据反应导航 5 我使用“路线”关键字来获取附加参数。 First of all I used首先我用

const {firstname} = route.params

it didn't worked so I tried logging the output of the params.它没有用,所以我尝试记录参数的输出。

const EditProfile = ({route}) => {
console.log(route.params);
console.log(route);
return(someJSX)

output:输出:

LOG      undefined
LOG      {"key": "EditProfile-eaMKo66zwdmYhxnS-uOdr", "name": "EditProfile", "params": undefined}

I am stuck in this since hours, help me thank you.几个小时以来,我一直被困在这个问题上,请帮助我,谢谢。

// 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/ https://www.positronx.io/react-native-stack-navigator-passing-getting-params-to-screen/

try hook useNavigation , react-navigation Version: 5.x尝试钩子 useNavigation ,react-navigation 版本:5.x

// 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
}
  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM