使用setParams和getParam将状态值传递给其他组件

问题描述 投票:0回答:1

我使用componentWillMount(),因为我认为无法使用static将setParams与状态以及GalleryScreen.navigationOptions = navData => {}class之外的React.Component一起使用。

我一直在尝试使用此方法,但总是收到以下错误:

TypeError:未定义不是对象(正在评估'props.navigation.state.params.imageData')]

我的结果是将state.pickedImage的值从GalleryScreen.js传递到return语句内的EventInput.js。

GalleyScreen.js:这是React组件

 componentWillMount() {
    const {setParams} = this.props.navigation;
    setParams({imageData: this.state.pickedImage})
}

EventInput.js:这只是EventInput道具(不是React组件)

return (
  <Image source={{uri: props.navigation.state.params.imageData}} style={{width: 300, height: 300}}/>
);

是否有其他解决方案,以便我可以传递state.pickedImage的值?并使用getParam从其他组件获取它的值?

reactjs react-native react-navigation
1个回答
0
投票

如果要访问导航,则应使用“ withNavigation”包装组件。

More details here

© www.soinside.com 2019 - 2024. All rights reserved.