如何在React天然中的两个屏幕之间传递参数?

发布于 2025-02-12 19:21:23 字数 1206 浏览 3 评论 0原文

请,不要急于将这个问题重复结束。我已经阅读了官方文档在这里,所以我设法在屏幕之间成功传递了参数。我目前的案件存在问题,解决方案对我来说并不明显。

我有两个屏幕。第一个称为“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舂唻埖巳落 2025-02-19 19:21:23

我认为我们通过路由访问参数我的意思是

const CommentsScreen =({route})=>{
    const {id} = route.params;
    console.log(id);
...}

I think we access params via route I mean

const CommentsScreen =({route})=>{
    const {id} = route.params;
    console.log(id);
...}
水晶透心 2025-02-19 19:21:23

访问传递的参数,

const CommentsScreen = (props) => {
  console.log("Comments Screen: ");
  console.log("comments: ", props.route.params.id);

您可以通过以下 props.route.params

其中包含您传递的对象,

navigation.navigation('screenname', {
...this object...
});

可以使用相同的键提取数据。

示例

navigation.navigation('screenname', {
  id: '1',
  name: 'Adam',
  age: 12
});

//Then you can access via 

  console.log("id: ", props.route.params.id);
  console.log("name: ", props.route.params.name);
  console.log("age: ", props.route.params.age);

You can access your passed params via following

const CommentsScreen = (props) => {
  console.log("Comments Screen: ");
  console.log("comments: ", props.route.params.id);

props.route.params

It contains the object you passed in

navigation.navigation('screenname', {
...this object...
});

you can extract the data by using the same keys.

Example

navigation.navigation('screenname', {
  id: '1',
  name: 'Adam',
  age: 12
});

//Then you can access via 

  console.log("id: ", props.route.params.id);
  console.log("name: ", props.route.params.name);
  console.log("age: ", props.route.params.age);
心房的律动 2025-02-19 19:21:23

我在这里找到了我问题的答案: https://reaectnavigation.org/docs/5.x/nesting-navigators#passing-params-params-to-a-a-screen-in-a-a-nested-navigator

在我的案例中:

navigation.navigate("Root Navigator", {
              screen: "Comments",
              params: { id: id },
            });

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:

navigation.navigate("Root Navigator", {
              screen: "Comments",
              params: { id: id },
            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文