Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Navigation go to same route with different params

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.

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

NavigationActions.navigate({ routeName: 'User', params: {userId} })
like image 745
Gerharddc Avatar asked Nov 01 '17 08:11

Gerharddc


People also ask

How do you pass a state in navigate in react?

To pass data when navigating programmatically with React Router, we can call navigate with an object. Then we can use the useLocation hook to return the object's data. const navigate = useNavigate(); navigate('/other-page', { state: { id: 7, color: 'green' } });


2 Answers

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.

See the documentation here 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'}) 
like image 67
tnemesis Avatar answered Sep 28 '22 02:09

tnemesis


Use the navigation key to navigate to same route

const navigateAction = NavigationActions.navigate({     routeName: 'User',      params: {userId},     key: 'APage' + APageUUID });  this.props.navigation.dispatch(navigateAction); 

OR

this.props.navigation.navigate({     routeName: 'User',      params: {userId},     key: 'APage' + APageUUID }); 

APageUUID is just a unique page id, can be generated with Math.random () * 10000

Reference

like image 35
TalESid Avatar answered Sep 28 '22 02:09

TalESid