Challenge - 5 Problems
Zustand Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output when updating Zustand state?
Given this Zustand store and React Native component, what will the displayed count be after pressing the button twice?
React Native
import create from 'zustand'; import React from 'react'; import { Text, Button } from 'react-native'; const useStore = create(set => ({ count: 0, increment: () => set(state => ({ count: state.count + 1 })) })); function Counter() { const { count, increment } = useStore(); return ( <> <Text>{count}</Text> <Button title="Add" onPress={increment} /> </> ); }
Attempts:
2 left
💡 Hint
Each button press calls increment which adds 1 to count.
✗ Incorrect
The increment function updates the count by adding 1 each time it is called. Pressing twice adds 2, so count becomes 2.
🧠 Conceptual
intermediate2:00remaining
Why choose Zustand over React Context for state management?
Which of these is a key advantage of Zustand compared to React Context API in React Native apps?
Attempts:
2 left
💡 Hint
Think about how components update when state changes.
✗ Incorrect
Zustand lets components subscribe to specific parts of the state, so only those components re-render when that part changes, improving performance.
❓ lifecycle
advanced2:00remaining
How does Zustand handle component unmounting?
What happens to Zustand store subscriptions when a React Native component using useStore unmounts?
Attempts:
2 left
💡 Hint
Think about React hooks cleanup behavior.
✗ Incorrect
Zustand uses React hooks internally which clean up subscriptions automatically when components unmount, preventing leaks.
🔧 Debug
advanced2:00remaining
Why does this Zustand state update not trigger re-render?
Consider this code snippet:
const useStore = create(set => ({ count: 0, setCount: (val) => set({ count: val }) }));
function Counter() {
const count = useStore(state => state.count);
const setCount = useStore(state => state.setCount);
React.useEffect(() => {
setCount(1);
setCount(1);
}, []);
return {count} ;
}
Why does the Text component not update after the second setCount call?
Attempts:
2 left
💡 Hint
Think about how Zustand detects state changes.
✗ Incorrect
Zustand only triggers re-render if the selected state slice changes. Setting count to the same value twice does not cause a re-render the second time.
expert
3:00remaining
How to persist Zustand state across React Native app restarts?
Which approach correctly persists Zustand state using AsyncStorage in React Native?
React Native
import create from 'zustand'; import AsyncStorage from '@react-native-async-storage/async-storage'; import { persist } from 'zustand/middleware'; const useStore = create(persist( (set) => ({ count: 0, increment: () => set(state => ({ count: state.count + 1 })) }), { name: 'count-storage', getStorage: () => AsyncStorage } ));
Attempts:
2 left
💡 Hint
Check zustand persist docs for React Native usage.
✗ Incorrect
Zustand's persist middleware supports AsyncStorage in React Native by passing getStorage returning AsyncStorage instance.