0
0
React Nativemobile~10 mins

Navigation state persistence in React Native - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the hook that helps save navigation state.

React Native
import { [1] } from '@react-navigation/native';
Drag options to blanks, or click blank then click option'
AuseNavigationContainerRef
BuseNavigationPersistence
CuseNavigationStatePersistence
DuseNavigationState
Attempts:
3 left
💡 Hint
Common Mistakes
Using a hook that does not exist in the library.
Confusing navigation state hooks with navigation container refs.
2fill in blank
medium

Complete the code to load the saved navigation state from async storage.

React Native
const loadState = async () => {
  const savedState = await AsyncStorage.[1]('NAVIGATION_STATE');
  if (savedState) {
    setInitialState(JSON.parse(savedState));
  }
};
Drag options to blanks, or click blank then click option'
AsetItem
BgetItem
CremoveItem
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using setItem instead of getItem to read data.
Trying to remove or clear storage instead of reading.
3fill in blank
hard

Fix the error in saving the navigation state when it changes.

React Native
const onStateChange = (state) => {
  AsyncStorage.[1]('NAVIGATION_STATE', JSON.stringify(state));
};
Drag options to blanks, or click blank then click option'
AgetItem
BmergeItem
CremoveItem
DsetItem
Attempts:
3 left
💡 Hint
Common Mistakes
Using getItem instead of setItem to save data.
Not converting state to a string before saving.
4fill in blank
hard

Fill both blanks to correctly initialize navigation container with persistence.

React Native
const navigationRef = [1]();

<NavigationContainer
  ref={navigationRef}
  initialState={initialState}
  onStateChange={onStateChange}
>[2]</NavigationContainer>
Drag options to blanks, or click blank then click option'
AuseNavigationContainerRef
BuseState
C<StackNavigator />
D<TabNavigator />
Attempts:
3 left
💡 Hint
Common Mistakes
Using useState instead of useNavigationContainerRef for the ref.
Placing no navigator or wrong navigator inside NavigationContainer.
5fill in blank
hard

Fill all three blanks to complete the async effect that loads saved navigation state on app start.

React Native
useEffect(() => {
  const restoreState = async () => {
    const savedState = await AsyncStorage.[1]('NAVIGATION_STATE');
    if (savedState) {
      setInitialState(JSON.[2](savedState));
    }
    setIsReady(true);
  };
  if (!isReady) {
    restoreState();
  }
}, [[3]]);
Drag options to blanks, or click blank then click option'
AgetItem
Bparse
CisReady
DsetItem
Attempts:
3 left
💡 Hint
Common Mistakes
Using setItem instead of getItem to read data.
Using JSON.stringify instead of JSON.parse to convert data.
Leaving dependency array empty causing multiple runs.