1

I'm trying to get initial state in the same format as onStateChange prop provides in NavigationContainer. onStateChange doesn't fire on initial render. In the example below, I managed to get state when navigating between screens, but not on the first one. Any ideas?

export default function App() {
  const [pageState, setPageState] = React.useState();

  return (
    <NavigationContainer onStateChange={(state) => setPageState(state)}>
      <RootNavigation />
    </NavigationContainer>
  );
}

1 Answer 1

2

Try using the onReady method from the NavigationContainer From the docs: https://reactnavigation.org/docs/navigation-container#onready

Sign up to request clarification or add additional context in comments.

2 Comments

onReady method doesn't receive the state, how can I use it
Combine it with the getRootState method. You just need to add a reference to the NavigatorContainer. Something like this: const navigationRef = useRef() const handleGetInitialState = () => { navigationRef.current?.getRootState() } <NavigationContainer ref={navigationRef} onReady={handleGetInitialState}>...</NavigationContainer>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.