通过this.props.navigation.getParam传递来自不同类的参数。 (React Native))

时间:2018-10-08 18:17:54

标签: reactjs react-native react-navigation

是否有可能在两个不同的js文件中的两个不同的类(父和子)之间传递数据作为参数。我将如何使用

()=>this.props.navigation.navigate('child', {
                name: 'muller',
                })

并通过

从子类中获取值

this.props.navigation.getParam('name')

尝试此操作时,我总是无法评估childClass中的this.props.navigation

下面是我实现了routestack的类

import { createStackNavigator } from 'react-navigation';

/* ROUTING RULES */
const RootStack = createStackNavigator(
    {
      Parent: Parent,
      Child: Child,
    },
    {
      initialRouteName: 'Parent',
     }
    

   
  );
/* End */
  

  export default class App extends React.Component {
    render() {
      return <RootStack />;
    }
  }

1 个答案:

答案 0 :(得分:0)

您可以通过this.props.navigation.state.params访问参数。 在您的情况下,它将是this.props.navigation.state.params.name

此外,如果您要访问navigation中未声明的组件中的RootStack对象,则应将navigation对象作为道具传递给该组件。另外,您也可以像这样使用 react-navigation 提供的withNavigation-

import { withNavigation } from 'react-navigation';

class YourComponent extends React.Component {
   ... your component ...
}

export default withNavigaiton(YourComponent);

这样YourComponent将可以访问navigation对象,从而可以访问参数。

希望这会有所帮助。