0
0
React Nativemobile~20 mins

Navigation state persistence in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Navigation State Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens to navigation state after app restart?

Consider a React Native app using React Navigation with state persistence enabled. The user navigates from Home to Profile screen. Then the app is closed and reopened.

What will be the visible screen after reopening?

AThe app shows the Home screen because navigation state is reset on restart.
BThe app shows the Profile screen because navigation state was saved and restored.
CThe app crashes because navigation state cannot be persisted.
DThe app shows a blank screen because state restoration failed silently.
Attempts:
2 left
💡 Hint

Think about what state persistence means for navigation.

🧠 Conceptual
intermediate
2:00remaining
Which method is used to save navigation state in React Navigation?

In React Navigation, to persist navigation state, which method is typically used to save the state externally?

AUsing <code>onStateChange</code> callback to save state to storage.
BUsing <code>useEffect</code> to reset navigation state on every render.
CUsing <code>navigation.reset()</code> to save state.
DUsing <code>componentDidMount</code> lifecycle method to save state.
Attempts:
2 left
💡 Hint

Look for a callback that triggers when navigation state changes.

lifecycle
advanced
2:00remaining
Why might navigation state restoration fail on app start?

Given this code snippet for restoring navigation state in React Native:

const [isReady, setIsReady] = React.useState(false);
const [initialState, setInitialState] = React.useState();

React.useEffect(() => {
  async function restoreState() {
    const savedState = await AsyncStorage.getItem('NAVIGATION_STATE');
    if (savedState) {
      setInitialState(JSON.parse(savedState));
    }
    setIsReady(true);
  }
  restoreState();
}, []);

if (!isReady) {
  return null;
}

return (
  <NavigationContainer initialState={initialState} onStateChange={...} >...
);

What is a common reason this restoration might silently fail and show the initial screen instead of the saved state?

AThe <code>initialState</code> is set after the NavigationContainer mounts, so it ignores it.
BAsyncStorage.getItem returns a promise that is never awaited.
CThe <code>isReady</code> state is set to true before <code>initialState</code> is set.
DThe saved state is not stringified before saving.
Attempts:
2 left
💡 Hint

Think about when NavigationContainer reads the initialState prop.

🔧 Debug
advanced
2:00remaining
What error occurs if navigation state is corrupted on restore?

If the saved navigation state in AsyncStorage is corrupted or incomplete JSON, what error will React Navigation most likely throw when trying to restore it?

AReferenceError: NavigationContainer is not defined
BTypeError: Cannot read property 'routes' of undefined
CSyntaxError: Unexpected token in JSON at position X
DRangeError: Maximum call stack size exceeded
Attempts:
2 left
💡 Hint

Consider what happens when JSON.parse fails.

🧠 Conceptual
expert
2:00remaining
How to optimize navigation state persistence for large apps?

In a large React Native app with many screens, persisting the entire navigation state can be slow and cause UI delays. Which approach best optimizes navigation state persistence?

APersist the full navigation state on every state change without throttling.
BPersist navigation state only when the app goes to background.
CDisable state persistence and rely on default navigation behavior.
DPersist only the minimal necessary parts of the navigation state, like current route name and params.
Attempts:
2 left
💡 Hint

Think about saving less data to improve performance.