0
0
React Nativemobile~20 mins

AsyncStorage for key-value in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
AsyncStorage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output when saving and retrieving a value with AsyncStorage?
Consider this React Native code snippet using AsyncStorage. What will be displayed in the Text component after pressing the button?
React Native
import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';

export default function App() {
  const [storedValue, setStoredValue] = useState('');

  const saveValue = async () => {
    await AsyncStorage.setItem('greeting', 'Hello!');
    const value = await AsyncStorage.getItem('greeting');
    setStoredValue(value || '');
  };

  return (
    <View>
      <Button title="Save and Load" onPress={saveValue} />
      <Text>{storedValue}</Text>
    </View>
  );
}
AThe Text component shows: undefined
BThe Text component shows: Hello!
CThe Text component shows: null
DThe Text component remains empty
Attempts:
2 left
💡 Hint
Remember AsyncStorage stores strings and getItem returns the stored string or null if not found.
lifecycle
intermediate
1:30remaining
When should you load AsyncStorage data in a React Native component?
You want to load a saved username from AsyncStorage when your screen appears. Which React hook is best to use for this?
AuseEffect with no dependency array
BuseState to load data directly
CuseMemo to load data once
DuseEffect with an empty dependency array []
Attempts:
2 left
💡 Hint
Think about running code once when the component mounts.
🔧 Debug
advanced
1:30remaining
What error does this AsyncStorage code produce?
Look at this code snippet. What error will it cause when run?
React Native
import AsyncStorage from '@react-native-async-storage/async-storage';

async function getData() {
  const value = await AsyncStorage.getItem('key');
  console.log(value);
}

getData();
AIt logs a Promise object instead of the stored value
BSyntaxError due to missing await keyword
CTypeError because getItem is not a function
DIt logs null if the key does not exist
Attempts:
2 left
💡 Hint
Remember AsyncStorage.getItem returns a Promise.
🧠 Conceptual
advanced
1:30remaining
How does AsyncStorage handle data types?
Which statement about AsyncStorage data storage is true?
AAsyncStorage stores only strings; other types must be converted before saving
BAsyncStorage stores data in binary format for efficiency
CAsyncStorage automatically serializes objects and arrays
DAsyncStorage supports storing numbers directly without conversion
Attempts:
2 left
💡 Hint
Think about what happens if you try to save an object directly.
navigation
expert
2:00remaining
What happens if you navigate away before AsyncStorage.setItem completes?
In a React Native app, you call AsyncStorage.setItem to save data, then immediately navigate to another screen. What is the likely outcome?
ANavigation waits automatically until setItem finishes saving
BThe data is always saved instantly before navigation happens
CThe data may not be saved because setItem is asynchronous and might be interrupted
DAn error is thrown if navigation happens before setItem completes
Attempts:
2 left
💡 Hint
AsyncStorage operations are asynchronous and do not block navigation.