reactjs - 使用 setParams 和 getParam 将 state 的值传递给其他组件
问题描述
我使用componentWillMount()
是因为我认为不可能将static
setParams 与GalleryScreen.navigationOptions = navData => {}
stateclass
以及React.Component
.
我一直在尝试使用这种方法,但我总是得到以下错误:
TypeError: undefined is not an object (评估'props.navigation.state.params.imageData')
我的结果是将 GalleryScreen.js 的值传递state.pickedImage
给返回语句中的 EventInput.js。
GalleyScreen.js :这是 React 组件
componentWillMount() {
const {setParams} = this.props.navigation;
setParams({imageData: this.state.pickedImage})
}
EventInput.js :这只是 EventInput 道具(不是 React 组件)
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
?并用于getParam
从其他组件获取它的价值?
解决方案
如果你想访问导航,你应该用“withNavigation”包装你的组件。
推荐阅读
- javascript - 在输入 react.js 时需要在输入字段中使用自动分隔符
- python - 如何在另一个之上绘制一个熊猫数据框(以一个为上限)
- go - 如何通过 corev1.PodLogOptions 从所有容器中获取日志?
- reactjs - React 使用 react-router 卸载和重新安装布局组件
- java - 无法在 onTouch 中按下时间
- android - 区分检查并单击单选按钮android
- email - Outlook 删除换行符 UTF-8 和纯文本邮件,无论配置如何
- python - 将两个 Numpy 数据集转换为特定的 PyTorch 数据集
- salesforce - 将应用程序与 Sage People (HRIS/HRMS) 集成
- go - 如何在 go 模板中转换给定的代码