如何在React天然中的两个屏幕之间传递参数?
请,不要急于将这个问题重复结束。我已经阅读了官方文档在这里,所以我设法在屏幕之间成功传递了参数。我目前的案件存在问题,解决方案对我来说并不明显。
我有两个屏幕。第一个称为“ feed”,它属于底部标签导航器。从“ feed”屏幕中,我希望能够导航到堆栈导航器的一部分的“注释”屏幕。
但是,由于某种原因,当我从“ feed”到“注释”时,我无法在两个屏幕之间成功导航。
我按“ feed”屏幕的一部分,我按“注释”图标:
<TouchableOpacity
onPress={() => {
console.log("passing param id: ", id);
navigation.navigate("Comments", { id: id });
}}
>
<FontAwesome name="comment-o" size={30} color="#0047AB" />
</TouchableOpacity>
我在控制台上看到的内容:
passing param id: some_id_whic_does_not_matter
我设法成功导航到“注释”屏幕。我的“评论”屏幕的一部分:
const CommentsScreen = (props) => {
console.log("Comments Screen: ");
console.log("comments: ", props);
我在路由对象的控制台上看到了什么:
route": Object {
"key": "Comments-some-key",
"name": "Comments",
"params": undefined,
}
您能告诉我我做错了什么吗?
我目前位于networkNavigator
选项卡上的位置,然后从那里导航到注释
屏幕。
我仍然看不到问题在哪里?
Please, do not hurry to close this question as duplicate. I have read the official documentation here and so fa I have managed to pass successfully parameters between screens. There is som problem with my current case, and the solution is not obvious for me.
I have two screens. The first is called "Feed" and it belongs to bottom tab navigator. From the "Feed" screen I want to be able to navigate to "Comments" screen which is part of stack navigator.
I am able to successfully navigate between both screens, however, for some reason I am not able to pass any parameters when I navigate from "Feed" to "Comments".
Part of my "Feed" screen where I press "comments" icon:
<TouchableOpacity
onPress={() => {
console.log("passing param id: ", id);
navigation.navigate("Comments", { id: id });
}}
>
<FontAwesome name="comment-o" size={30} color="#0047AB" />
</TouchableOpacity>
What I see on the console:
passing param id: some_id_whic_does_not_matter
I manage to successfully navigate to my "Comments" screen. Part of my "Comments" screen:
const CommentsScreen = (props) => {
console.log("Comments Screen: ");
console.log("comments: ", props);
What I see on the console for the route object:
route": Object {
"key": "Comments-some-key",
"name": "Comments",
"params": undefined,
}
Can you tell me what I am doing wrong?
Where I am currently located on NetworkNavigator
tab, and from there I want to navigate to the Comments
screen.
I still dont see where the issue is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为我们通过
路由访问参数
我的意思是I think we access params via
route
I mean访问传递的参数,
您可以通过以下 props.route.params
其中包含您传递的对象,
可以使用相同的键提取数据。
示例
You can access your passed params via following
props.route.params
It contains the object you passed in
you can extract the data by using the same keys.
Example
我在这里找到了我问题的答案: https://reaectnavigation.org/docs/5.x/nesting-navigators#passing-params-params-to-a-a-screen-in-a-a-nested-navigator
在我的案例中:
I found the answer to my question here: https://reactnavigation.org/docs/5.x/nesting-navigators#passing-params-to-a-screen-in-a-nested-navigator
So in my case: