AsyncStorage is a simple, unencrypted, asynchronous, persistent, key-value storage system for React Native apps. It lets you save data locally on the device.
Click to reveal answer
beginner
How do you save a value with AsyncStorage?
Use AsyncStorage.setItem(key, value) to save a string value under a key. It returns a Promise that resolves when the data is saved.
Click to reveal answer
beginner
How do you read a value from AsyncStorage?
Use AsyncStorage.getItem(key) to get the value stored under the key. It returns a Promise that resolves to the string value or null if not found.
Click to reveal answer
intermediate
Why is AsyncStorage asynchronous?
AsyncStorage is asynchronous to avoid blocking the app’s user interface while reading or writing data, keeping the app smooth and responsive.
Click to reveal answer
intermediate
Can AsyncStorage store objects directly?
No, AsyncStorage stores only strings. To save objects, convert them to strings using JSON.stringify before saving, and parse them back with JSON.parse when reading.
Click to reveal answer
Which method saves data in AsyncStorage?
AgetItem
BsetItem
CremoveItem
Dclear
✗ Incorrect
setItem is used to save a key-value pair in AsyncStorage.
What type of data does AsyncStorage store?
AOnly strings
BOnly numbers
CObjects directly
DBinary files
✗ Incorrect
AsyncStorage stores only strings. Objects must be converted to strings first.
Why should AsyncStorage operations be asynchronous?
ATo speed up network calls
BTo save battery
CTo encrypt data
DTo keep the app responsive
✗ Incorrect
AsyncStorage is asynchronous to avoid blocking the UI thread and keep the app smooth.
How do you retrieve a value from AsyncStorage?
AgetItem(key)
BsetItem(key)
CfetchItem(key)
DloadItem(key)
✗ Incorrect
getItem(key) returns the stored string value for the given key.
What should you do before saving an object in AsyncStorage?
AEncrypt it
BCompress it
CConvert it to a string with JSON.stringify
DConvert it to a number
✗ Incorrect
Objects must be converted to strings using JSON.stringify before saving.
Explain how to save and retrieve a user name using AsyncStorage in React Native.
Think about saving a string under a key and reading it back asynchronously.
You got /5 concepts.
Describe why AsyncStorage uses asynchronous methods and how that benefits app performance.
Consider what happens if storage was synchronous and how it affects the app.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of AsyncStorage in React Native?
easy
A. To create complex animations in the app
B. To handle network requests and API calls
C. To store small amounts of data as key-value pairs on the device
D. To manage app navigation between screens
Solution
Step 1: Understand AsyncStorage's role
AsyncStorage is designed to save simple data like user preferences as key-value pairs on the device.
Step 2: Compare with other options
Animations, network requests, and navigation are handled by other libraries or APIs, not AsyncStorage.
Final Answer:
To store small amounts of data as key-value pairs on the device -> Option C
Quick Check:
AsyncStorage = key-value storage [OK]
Hint: AsyncStorage saves simple data locally as key-value pairs [OK]
Common Mistakes:
Confusing AsyncStorage with network or UI libraries
Thinking AsyncStorage manages navigation or animations
2. Which of the following is the correct way to save a value using AsyncStorage in React Native?
easy
A. await AsyncStorage.setItem('key', 'value');
B. AsyncStorage.save('key', 'value');
C. AsyncStorage.store('key', 'value');
D. await AsyncStorage.put('key', 'value');
Solution
Step 1: Recall AsyncStorage method for saving data
The correct method to save data is setItem, which returns a promise and requires await.
Step 2: Check method names
Methods like save, store, and put do not exist in AsyncStorage API.
Final Answer:
await AsyncStorage.setItem('key', 'value'); -> Option A
Quick Check:
Save data = setItem [OK]
Hint: Use setItem with await to save data [OK]
Common Mistakes:
Using incorrect method names like save or put
Forgetting to use await with async calls
3. What will be logged to the console after running this code?
import AsyncStorage from '@react-native-async-storage/async-storage';
async function test() {
await AsyncStorage.setItem('name', 'Alice');
const value = await AsyncStorage.getItem('name');
console.log(value);
}
test();
medium
A. Alice
B. null
C. Error: AsyncStorage not found
D. undefined
Solution
Step 1: Analyze setItem and getItem usage
The code saves the string 'Alice' under the key 'name' and then retrieves it correctly using await.
Step 2: Understand console output
Since the value was saved and retrieved successfully, console.log prints 'Alice'.
Final Answer:
Alice -> Option A
Quick Check:
Saved value retrieved = Alice [OK]
Hint: getItem returns saved string or null if missing [OK]
Common Mistakes:
Expecting undefined instead of null or string
Not awaiting async calls causing errors
4. Identify the error in this AsyncStorage usage:
import AsyncStorage from '@react-native-async-storage/async-storage';
function saveData() {
AsyncStorage.setItem('token', 'abc123');
console.log('Token saved');
}
saveData();
medium
A. console.log should be inside a .then() callback
B. Wrong method name setItem should be saveItem
C. AsyncStorage must be imported from 'react-native' not '@react-native-async-storage/async-storage'
D. Missing await before AsyncStorage.setItem causing unhandled promise
Solution
Step 1: Check async usage of setItem
AsyncStorage.setItem returns a promise and should be awaited or handled with then to ensure completion.
Step 2: Identify missing await
The function saveData is not async and does not await setItem, so the promise is unhandled and may cause issues.
Final Answer:
Missing await before AsyncStorage.setItem causing unhandled promise -> Option D