将本机传递的数据反应到另一个屏幕

时间:2021-06-25 16:12:22

标签: react-native parameter-passing react-navigation react-navigation-v5

我需要将数据传递到另一个屏幕。这是导航堆栈中的代码

 <Stack.Screen name="MyScreen" component={MyScreen} options={({ route }) => ({ title: 'Profile', url: 'http://www.google.com' })} />

在另一个屏幕中,这是我的代码,但我看不到参数。

import { Alert, View, Text, StyleSheet,} from 'react-native';

export default class MyScreen extends Component {

    render() {
        const { navigation } = this.props;
        let mydata = navigation.getParam('url', 'http://');
        alert(mydata);


    }
}

1 个答案:

答案 0 :(得分:0)

在 react-navigation v5 中

你可以像这样设置initialParams

<Stack.Screen
  name = "MyScreen"
  component = {MyScreen}
  initialParams = {{
    myParam: 'Example value from param',
    myAnotherParam: 5
  }}
/>

然后在你的屏幕脚本中,你可以像这样访问 myParam 或 myAnotherParam

this.props.route.params.myParam // 'Example value from param'

this.props.route.params.myAnotherParam // 5