Reactjs 使用setParams和getParam将状态值传递给其他组件
我使用Reactjs 使用setParams和getParam将状态值传递给其他组件,reactjs,react-native,react-navigation,Reactjs,React Native,React Navigation,我使用componentWillMount()是因为我认为在React.Component的类之外,不可能使用static对状态和GalleryScreen.navigationOptions=navData=>{}使用setParams 我一直在尝试使用这种方法,但我总是得到以下错误: TypeError:undefined不是对象(正在计算'props.navigation.state.params.imageData') 我的结果是在返回语句中将state.pickedImage的值从Ga
componentWillMount()
是因为我认为在React.Component
的类
之外,不可能使用static
对状态和GalleryScreen.navigationOptions=navData=>{}
使用setParams
我一直在尝试使用这种方法,但我总是得到以下错误:
TypeError:undefined不是对象(正在计算'props.navigation.state.params.imageData')
我的结果是在返回语句中将state.pickedImage
的值从GalleryScreen.js传递到EventInput.js
GalleyScreen.js:这是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);
EventInput.js:这只是EventInput道具(不是React组件)
const EventInput=props=>{
常量[标题值,设置标题值]=使用状态(“”);
常量[descriptionValue,setDescriptionValue]=useState(“”);
//const imageData=props.navigation.getParam('imageData');
const events=useSelector(state=>state.events.events);
常量标题更改处理程序=(标题)=>{
setTitleValue(标题);
};
常量descriptionChangeHandler=(描述)=>{
setDescriptionValue(描述);
};
返回(
事件输入
{
props.navigation.navigate('SetEventLocation'{
活动名称:titleValue,
eventDescription:descriptionValue
})
}}>
下一个
);
};
使用导航导出默认值(事件输入);
是否有其他解决方案可以让我传递我的
状态值。PickeImage
?并使用getParam
从其他组件获取其值?如果要访问导航,应使用“withNavigation”包装组件
不清楚你在问什么。您想将参数传递给
导航选项
还是另一个组件?另一个组件,我想在返回语句中将其从GalleryScreen.js传递给EventInput.js您能共享EventInput.js的代码吗?它是一个无状态函数还是一个函数?谢谢,您可以通过以下方式获取ImageData:props.navigation.getParam(“ImageData”)不起作用。为了确保它不起作用,我将该值更改为文本并显示为文本,但仍然没有成功。是否将导航道具传递给EventInput?您可以检查此链接如何执行此操作。