我使用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
从其他组件获取它的值?