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

时间:2019-11-18 15:40:49

标签: reactjs react-native react-navigation

我使用componentWillMount()是因为我认为无法使用static在状态GalleryScreen.navigationOptions = navData => {}的{​​{1}}外使用setParams和class

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

  

TypeError:undefined不是对象(正在评估“ props.navigation.state.params.imageData”)

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

GalleyScreen.js:这是React组件

state.pickedImage

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

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

是否有其他解决方案,以便我可以传递const EventInput = props => { const [titleValue, setTitleValue] = useState(''); const [descriptionValue, setDescriptionValue] = useState(''); // const imageData = props.navigation.getParam('imageData'); const events = useSelector(state => state.events.events); const titleChangeHandler = (title) => { setTitleValue(title); }; const descriptionChangeHandler = (description) => { setDescriptionValue(description); }; return ( <View style={styles.container}> <StatusBar backgroundColor='transparent' translucent barStyle='dark-content'/> <Text>Event Input</Text> <TextInput placeholder="Enter title..." onChangeText={titleChangeHandler} /> <TextInput placeholder="Enter description..." onChangeText={descriptionChangeHandler} /> <Image source={{uri: props.navigation.state.params.imageData}} style={{width: 300, height: 300}}/> <TouchableOpacity onPress={() => { props.navigation.navigate('SetEventLocation', { eventTitle: titleValue, eventDescription: descriptionValue }) }}> <Text>Next</Text> </TouchableOpacity> </View> ); }; export default withNavigation(EventInput); 的值?并使用state.pickedImage从其他组件获取它的值?

1 个答案:

答案 0 :(得分:0)

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

More details here