3

Current behavior

In my team we would like to upgrade react navigation to V5. Since our codebase is very large we have to do an incremental refactor (Upgrading everything in one shot is impossible).

For this reason we would like to use the helper @react-navigation/compat with the latest version ^5.3.20. So far this is the different piece of code we have updated:

Main app navigation: At start we were using createSwitchNavigator. At startup we check if user is logged in:

  • If yes: redirect to MainNavigation navigator.
  • if no: redirect to Authentification navigator.
<NavigationContainer>
    <Main.Navigator>
        <Main.Screen
          name={SceneKeys.Startup}
          component={Startup}
          options={{ header: () => null }}
        ></Main.Screen>

        { !authenticated ? (
          <Main.Screen
            name={SceneKeys.Authentication}
            component={AuthenticationNavigation}
            options={{ header: () => null }}
          ></Main.Screen>
        ) : (
          <Main.Screen
            name={SceneKeys.App}
            component={MainNavigation}
            options={{ header: () => null }}
          ></Main.Screen>
        )}
      </Main.Navigator>
   </NavigationContainer>

This part work well, if user is not logged in we are correctly redirected to AuthenticationNavigation. The problem is in our main navigation.

const MainNavigation = createCompatNavigatorFactory(createStackNavigator)(
  {
    // Main stack contain many navigator (switch, drawer, etc...)
    MainStack: { screen: TabBarNavigator, navigationOptions: { header: null } },
    // ...other routes
  }

When we redirect on this navigator we have some errors coming from withNavigation (in compat mode) and navigationOptions.

withNavigation: The error say that we cannot use withNavigation since it's a hook and we can only call this in the body of a function. However we use the compat' mode and on the doc it says that is exported as a HOC.

export default withNavigation(
  connect(mapStateToProps, mapDispatchToProps)(OurComponent),
);

Header: Header props fail even with the compat' mode with navigationOptions and createCompatNavigatorFactory

const MessagingNavigator = createCompatNavigatorFactory(createStackNavigator)(
  {
    [SceneKeys.messaging]: {
      screen: Messaging,
      navigationOptions: ({ navigation }) => ({
        header: (
          <MessagingNavBar mode={'conversationsList'} navigation={navigation} />
        ),
        drawerLockMode: navigation.state.params
          ? navigation.state.params.drawerLockMode
          : undefined,
      }),
    },

If I delete navigationOptions property and I refactor withNavigation by using useNavigation() hook it works well and go to the correct screen otherwise it doesn't work.

At the moment we cannot afford to refactor everything from the ground up and we would like to do that incrementally. With the informations above, did we miss something ?

Expected behavior

  • Is withNavigation should work as an HOC's with component and redux-connect since it's exported from compat' plugin ?
  • Why navigationOptions not working with the previous headers implementation in compatibility mode?
  • Did you check this document? https://reactnavigation.org/docs/5.x/redux-integration/ – Rajendran Nadar Aug 23 '22 at 14:40
  • Also, you must check this upgrading guide! https://reactnavigation.org/docs/5.x/upgrading-from-4.x/ – Rajendran Nadar Aug 23 '22 at 14:42
  • Hello @RajendranNadar, thank you for your answer, I've read the doc but we don't want to use the upgrade as is, we need to do that incrementally. We use more than 200 screens and use the navigationOptions props a lot. It's not doable in one PR so we want to do it in multiples. That's why we want to use the compatibility layer: https://reactnavigation.org/docs/5.x/compatibility/ but it seems that withNavigation HOC and navigationOptions not working as expected ... – Jérémie Bardon Aug 23 '22 at 21:22
  • When you say not working as expected can you send a reproduction of the issue? – Rajendran Nadar Aug 24 '22 at 09:27
  • If you read this doc https://reactnavigation.org/docs/5.x/compatibility/ you need to do this setup https://reactnavigation.org/docs/5.x/getting-started/ and migrate the packages to v5 and follow https://reactnavigation.org/docs/5.x/upgrading-from-4.x/, this doesn’t mean you need to migrate everything – Rajendran Nadar Aug 24 '22 at 09:50
  • @RajendranNadar Yes I have done that already, the problem is the compat mode who do not integration screenOptions and withNavigation who act like a hooks and not an HOC. Since it's should be according to the compatibility layer doc – Jérémie Bardon Aug 26 '22 at 17:00

0 Answers0