expo / router

[ARCHIVE]: Expo Router has moved to expo/expo -- The File-based router for universal React Native apps
https://docs.expo.dev/routing/introduction/
1.36k stars 113 forks source link

Migration to expo-router-v2 breaks onLayout redirect #848

Closed zRelux closed 1 year ago

zRelux commented 1 year ago

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

npm

Summary

I had this hook on the root layout which is used to determine the first route of the app. (expo-router v1.7)

  useLayoutEffect(() => {
    const checkState = () => {
      if (!navigationState?.key) {
        // Temporary fix for router not being ready.
        return;
      }

      if (!isLoggedIn) {
        replace('(no-auth)/sign-in');

        return;
      }

      if (locationGranted === undefined) {
        replace('(no-auth)/sign-in/location');

        return;
      }

      if (notificationGranted === 'undetermined') {
        replace('(no-auth)/sign-in/notification');

        return;
      }

      if (preferences.length === 0) {
        replace('(no-auth)/sign-in/preferences');

        return;
      }

      replace('(app)/(tabs)/dream');
    };

    checkState();
  }, [
    isLoggedIn,
    locationGranted,
    navigationState?.key,
    notificationGranted,
    preferences.length,
    replace,
  ]);

then I upgraded to expo-router 2.0.2 and this doesn't work anymore because for some reason navigationState?.key keeps updating causing infinte render I tried updating the code to use rootNavigation.isReady() and the problem with that is that the app redirect after some seconds leaving me waiting in front of the no matched route screen. What should I do?

Minimal reproducible example

Code provided above

marklawlor commented 1 year ago

Can you provide a repo so we can easily test your issue?

zRelux commented 1 year ago

Managed to find a fix, with expo router 2.0.4 navigationState?.key check is not needed anymore