Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossible to have a navigate to a route multiple times #758

Closed
jechazelle opened this issue Jul 9, 2023 · 14 comments · Fixed by expo/expo#24600
Closed

Impossible to have a navigate to a route multiple times #758

jechazelle opened this issue Jul 9, 2023 · 14 comments · Fixed by expo/expo#24600
Labels
documentation Improvements or additions to documentation

Comments

@jechazelle
Copy link

Which package manager are you using? (Yarn is recommended)

npm

Summary

Hi,

Thank you so much for your job and expo router!

I tried expo router and I have an issue: I can't navigate to a route multiple times as react navigation (Navigate to a route multiple times):

stack-push

Minimal reproducible example

I tried with this code, but not work:

import { View, Text, Button } from 'react-native'
import React from 'react'
import { useRouter } from 'expo-router';

export default function settings() {
  const router = useRouter();

  return (
    <View>
      <Text>settings</Text>

      <Button title="Setings again" onPress={() => router.push('/home/settings')} />
    </View>
  )
}
@marklawlor marklawlor added the documentation Improvements or additions to documentation label Jul 11, 2023
@feRpicoral
Copy link

Have you tried using a Link as the docs state? https://docs.expo.dev/routing/navigating-pages/

@jechazelle
Copy link
Author

jechazelle commented Jul 11, 2023

@feRpicoral same thing when I use Link.

After the first click on Settings again and go to /home/settings page, I can't click again on Settings again, nothing is happening.

@kennethstarkrl
Copy link

This is also an issue when using dynamic routes. The url will update but the page does nothing.
Had to set the path name with usePathname() hook to a state then a useEffect with the path as a dependency to refresh the data.
Don't think that would work tho when the path doesn't change.

@jechazelle
Copy link
Author

jechazelle commented Jul 13, 2023

@kennethstarkrl do you have a fix or some code to fix it please?

I opened an issue #768 with dynamic routes. But same problem after the fix (#713). I think this problem is the same problem.

@kennethstarkrl
Copy link

kennethstarkrl commented Jul 13, 2023

@jechazelle

const { id } = useLocalSearchParams();
const [data,setData] = useState(null);
useEffect(()=>{
   //clear current data
   setData(null);
   //get the new data
   setData(newData);
},[id]);

The useEffect fires when the id changes. Not sure if that will work for paths where the url doesn't change. Might be able to just do the whole router as the dependency..

const router = useRouter();
const [data,setData] = useState(null);
useEffect(()=>{
   //clear current data
   setData(null);
   //get the new data
   setData(newData);
},[router]);

This is not a solution but a work around.

@bhyoo99
Copy link

bhyoo99 commented Jul 26, 2023

Any updates?

@ozio
Copy link

ozio commented Aug 7, 2023

Ah, my issue could be the same bug. As I mentioned there, it happens only when I override default <Stack.Screen /> options in the _layout.js. So, I think there is something in default Stack.Screen props that could fix it, but I didn't find it in the project code. Could somebody help me with that?

@snowdigital
Copy link

snowdigital commented Aug 9, 2023

Check
#747
#9090

Close issue due to duplication

for lazy ones code to solve error is

for those who facing the same issue use following code

<Stack.Screen getId={({ params }) => params.id} name="user/[id]" />

getID function makes it work

dont forget to spread params

@Seimodei
Copy link

Upgrading expo-router to latest version (2.0.4 as at this comment) solves this issue.

@Nziranziza
Copy link

I am also facing a similar issue and I am using the latest version 2.0.5

@Tobjoern
Copy link

I'm using expo-router 2..0.9 with a route, which doesn't have any props, the 'push' method doesn't work for me,
as it has the same behviour the 'navigate' method has.

@ludwig-pro
Copy link

expo-router overrides the dispatch action type from a PUSH to NAVIGATE. use this patch at your own risk...

// expo-router+2.0.12.patch

diff --git a/node_modules/expo-router/src/global-state/routing.ts b/node_modules/expo-router/src/global-state/routing.ts
index e005d1a..413e2c6 100644
--- a/node_modules/expo-router/src/global-state/routing.ts
+++ b/node_modules/expo-router/src/global-state/routing.ts
@@ -19,7 +19,7 @@ function assertIsReady(store: RouterStore) {
 }
 
 export function push(this: RouterStore, url: Href) {
-  return this.linkTo(resolveHref(url));
+  return this.linkTo(resolveHref(url), "PUSH");
 }
 
 export function replace(this: RouterStore, url: Href) {
@@ -120,7 +120,7 @@ export function linkTo(this: RouterStore, href: string, event?: string) {
     case "REPLACE":
       return navigationRef.dispatch(getNavigateReplaceAction(state, rootState));
     default:
-      return navigationRef.dispatch(getNavigatePushAction(state, rootState));
+      return navigationRef.dispatch(getNavigatePushAction(state, rootState, event));
   }
 }
 
@@ -149,10 +149,11 @@ function rewriteNavigationStateToParams(
   return JSON.parse(JSON.stringify(params));
 }
 
-function getNavigatePushAction(state: ResultState, rootState: NavigationState) {
+function getNavigatePushAction(state: ResultState, rootState: NavigationState, type = "NAVIGATE") {
   const { screen, params } = rewriteNavigationStateToParams(state);
+
   return {
-    type: "NAVIGATE",
+    type,
     target: rootState.key,
     payload: {
       name: screen,

@Julian03-lab
Copy link

The thing what work for me was update the version of expo router (from 2.0.0 to 2.0.5)

EvanBacon added a commit to expo/expo that referenced this issue Dec 15, 2023
# Why

`router.push()` should always most history forwards. However we are
currently using the `NAVIGATE` action which may move history backwards
if the screen is in history and the closest navigator is a Stack.

Adds `router.navigate` for when users wish to opt out of this behaviour.

Fix: expo/router#758
Fix: expo/router#741

Keeping this in draft until #24598 is
merged

# How

This changes `router.push()` to a PUSH action if the lowest nested
navigator is a stack and shared between the old & new routes. Otherwise
it defaults back to NAVIGATE.

# Test Plan

Included test case

# Checklist

- [ ] Documentation is up to date to reflect these changes (eg:
https://docs.expo.dev and README.md).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).

---------

Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
Co-authored-by: Evan Bacon <bacon@expo.io>
Co-authored-by: Aman Mittal <amandeepmittal@live.com>
Co-authored-by: Bartosz Kaszubowski <gosimek@gmail.com>
@louielyl
Copy link

louielyl commented Jan 1, 2024

For future readers: I came to this post for the same problem, but updating the package did not work for me.
However, I got it working after installing "expo-router": "^2.0.14" and rebuild my project.

onizam95 pushed a commit to onizam95/expo-av-drm that referenced this issue Jan 15, 2024
…4600)

# Why

`router.push()` should always most history forwards. However we are
currently using the `NAVIGATE` action which may move history backwards
if the screen is in history and the closest navigator is a Stack.

Adds `router.navigate` for when users wish to opt out of this behaviour.

Fix: expo/router#758
Fix: expo/router#741

Keeping this in draft until expo#24598 is
merged

# How

This changes `router.push()` to a PUSH action if the lowest nested
navigator is a stack and shared between the old & new routes. Otherwise
it defaults back to NAVIGATE.

# Test Plan

Included test case

# Checklist

- [ ] Documentation is up to date to reflect these changes (eg:
https://docs.expo.dev and README.md).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).

---------

Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
Co-authored-by: Evan Bacon <bacon@expo.io>
Co-authored-by: Aman Mittal <amandeepmittal@live.com>
Co-authored-by: Bartosz Kaszubowski <gosimek@gmail.com>
DavidAmyot pushed a commit to Villeco-inc/expo-router that referenced this issue Oct 16, 2024
# Why

`router.push()` should always most history forwards. However we are
currently using the `NAVIGATE` action which may move history backwards
if the screen is in history and the closest navigator is a Stack.

Adds `router.navigate` for when users wish to opt out of this behaviour.

Fix: expo/router#758
Fix: expo/router#741

Keeping this in draft until expo/expo#24598 is
merged

# How

This changes `router.push()` to a PUSH action if the lowest nested
navigator is a stack and shared between the old & new routes. Otherwise
it defaults back to NAVIGATE.

# Test Plan

Included test case

# Checklist

- [ ] Documentation is up to date to reflect these changes (eg:
https://docs.expo.dev and README.md).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).

---------

Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
Co-authored-by: Evan Bacon <bacon@expo.io>
Co-authored-by: Aman Mittal <amandeepmittal@live.com>
Co-authored-by: Bartosz Kaszubowski <gosimek@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.