Challenge - 5 Problems
AsyncStorage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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> ); }
Attempts:
2 left
💡 Hint
Remember AsyncStorage stores strings and getItem returns the stored string or null if not found.
✗ Incorrect
The code saves the string 'Hello!' under the key 'greeting'. Then it retrieves it immediately and sets it to state. So the Text component displays 'Hello!'.
❓ lifecycle
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about running code once when the component mounts.
✗ Incorrect
useEffect with [] runs once when the component mounts, perfect for loading data from AsyncStorage once on screen load.
🔧 Debug
advanced1: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();
Attempts:
2 left
💡 Hint
Remember AsyncStorage.getItem returns a Promise.
✗ Incorrect
Without await, value is a Promise object, so console.log prints the Promise, not the resolved string.
🧠 Conceptual
advanced1:30remaining
How does AsyncStorage handle data types?
Which statement about AsyncStorage data storage is true?
Attempts:
2 left
💡 Hint
Think about what happens if you try to save an object directly.
✗ Incorrect
AsyncStorage stores only strings. To save objects or arrays, you must convert them to strings (e.g., JSON.stringify) before saving.
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?
Attempts:
2 left
💡 Hint
AsyncStorage operations are asynchronous and do not block navigation.
✗ Incorrect
AsyncStorage.setItem is asynchronous and does not block navigation. If you navigate away immediately, the save might not complete before the app state changes.