【问题标题】:React native passing data to another screen将本机传递数据反应到另一个屏幕
【发布时间】:2021-09-09 00:44:57
【问题描述】:

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

 <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);


    }
}

【问题讨论】:

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


    【解决方案1】:

    在反应导航 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多