expo / router

[ARCHIVE]: Expo Router has moved to expo/expo -- The File-based router for universal React Native apps

Home Page:https://docs.expo.dev/routing/introduction/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Migration to expo-router-v2 breaks onLayout redirect

zRelux opened this issue · comments

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

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

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