React-Navigation 使用不同的参数转到相同的路线 [英] React-Navigation go to same route with different params

查看:31
本文介绍了React-Navigation 使用不同的参数转到相同的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用名为User"的 React-Navigation 屏幕来显示有关特定用户的信息.当导航到这条路线时,我传递了参数id",它告诉屏幕它正在处理什么用户.查看某个用户时,可以单击某些内容让您查看其他用户.

I am using a React-Navigation screen with the name "User" to show info about a particular user. When navigating to this route I pass the param "id" which tells the screen what user it is dealing with. When viewing a user it is possible to click something that will let you view another user.

目前这可行,新参数使其他用户的详细信息显示出来.问题在于它不会为其他用户导航到新屏幕,而只是更改当前屏幕.问题是,当您向后导航时,它不会将您带到初始用户的屏幕,而是它之前的任何屏幕.您也不会获得导航的视觉提示.

At the moment this works and the new param makes the other user's details show up. The problem is that it does not navigate to a new screen for the other user and instead just changes the current screen. The problem is that when you navigate back it does not take you to the screen for the initial user but whatever was before it. You also get no visual cue that you navigated.

因此,我想知道如何强制它导航到同一路线的另一个版本.

I would thus like to know how I can force it to navigate to another version of the same route.

如果有什么不同,我正在使用 Redux 并通过分派生成的操作进行导航,例如:

If it makes any difference, I am working with Redux and navigate by dispatching generated actions like:

NavigationActions.navigate({ routeName: 'User', params: {userId} })

推荐答案

您正在寻找 push 而不是 navigate.当您使用导航时,它会查找具有该名称的路线,如果存在,则导航到该路线.当您使用推送时,您会转到一条新路线,向堆栈添加一个新导航.

You are looking for push instead of navigate. When you use navigate, it look for a route with that name, and if it exists navigate to it. When you use push, you go to a new route, adding a new navigation to the stack.

请参阅此处的文档 https://reactnavigation.org/docs/en/navigating.html#navigate-to-a-route-multiple-times

在你的情况下,你应该这样做:

In your case, you should do:

NavigationActions.push({ routeName: 'User', params: {userId} }) 

或者通过你的道具(确保你的道具有导航"):

or through your props (make sure your props has 'navigation'):

this.props.navigation.push('User', {userId:'paramValue'})

这篇关于React-Navigation 使用不同的参数转到相同的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆