是否有可能在两个不同的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 />;
}
}
答案 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
对象,从而可以访问参数。
希望这会有所帮助。