Rohit Aggarwal
Rohit Aggarwal

Reputation: 1188

getStateForAction is undefined for react-navigation v6

Currently I am upgrading my react-navigation from v4 to v6. But I am facing problem while getting initial state for my App.

// AppNavigation.js
// All the screens and packages are imported
function switchNavigator() {
  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName="loginFlow">
        <Stack.Screen name="loginFlow" component={loginFlow} />
        <Stack.Screen name="mainFlow" component={mainFlow} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}
export default switchNavigator;

Now I am using the below code to get the initial state and set it if it's undefined in my redux navigation.

import AppNavigation from '../Navigation/AppNavigation';

export const reducer = (state, action) => {
  const newState = AppNavigation.router.getStateForAction(action, state);
  return newState || state;
};

But I am getting this error

TypeError: undefined is not an object (evaluating '_AppNavigation.default.router.getStateForAction')

Upvotes: 1

Views: 960

Answers (1)

Jignesh Mayani
Jignesh Mayani

Reputation: 7213

Here is the Awesome feature that comes from the React Navigation Library With Which We are able to use the navigation options without accessing the Props as the components do.

Please refer to the Below Documentation page from the React Navigation, I hope it might help you in your achieving.

React Navigation Accessing Navigation Without Props

Upvotes: 2

Related Questions