如何将数据传递给先前的屏幕,并使用React导航6。
我正在尝试将所选的数据传递给我从REST API获取的数据回到上一个屏幕。我关注了文档,但它不起作用。不知道我在做什么错。
我正在使用选定的数据更新状态,并且知道它正在运行BC,我正在成功地将其打印出来。
这是调用函数导航的按钮,在屏幕B中传递了带有所选数据的状态:
<Pressable onPress={() => postSelected(selectedData)} >
<Text style={{fontSize: 13, color: 'white', fontWeight: '700', paddingRight: 5}}>{'Select'}</Text>
</Pressable>
这是函数(屏幕B):
const postSelected = (selectedData) => {
navigation.navigate({
name: 'CreatePost',
params: { postData: selectedData },
merge: true
});
}
在屏幕A中,我有一个使用效率,可以从屏幕B:
useEffect(() => {
if (route.params?.postData) {
console.log('Sent');
}
}, [route.params?.postData]);
但这没有收到数据。
我正在关注这些文档: https://reactnavigation..org/docs/params/params/
呢
I am trying to pass the selected data i'm fetching from my rest api back to the previous screen. I followed the docs, but it's not working. Not sure what I am doing wrong.
I am updating the state w/ the selected data, and know it's working bc i am printing it out in console successfully.
This is the button that calls the function to navigate back, passing the state w/ selected data in Screen B:
<Pressable onPress={() => postSelected(selectedData)} >
<Text style={{fontSize: 13, color: 'white', fontWeight: '700', paddingRight: 5}}>{'Select'}</Text>
</Pressable>
This is the function (Screen B):
const postSelected = (selectedData) => {
navigation.navigate({
name: 'CreatePost',
params: { postData: selectedData },
merge: true
});
}
In Screen A, I have a useEffect that listens for the selected data from Screen B:
useEffect(() => {
if (route.params?.postData) {
console.log('Sent');
}
}, [route.params?.postData]);
But it's not receiving the data.
I was following these docs: https://reactnavigation.org/docs/params/
Appreciate any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用props.navigation.getParam(“ postdata”)方法从导航中获取参数
You can use props.navigation.getParam("postData") Method to get params from navigation
对我有用并遵循React-Navigation设定的最佳实践的解决方案如下
The solution that worked for me and follows the best practices set by react-navigation is as follows