0
0
React Nativemobile~20 mins

State management comparison in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
State Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding useState vs useReducer
Which statement best describes when to prefer useReducer over useState in React Native?
AUse useReducer when state logic is complex and involves multiple sub-values or when the next state depends on the previous one.
BUse useReducer only for managing global app state across multiple screens.
CUse useReducer when you want to update state directly without dispatching actions.
DUse useReducer when you want to avoid using any functions to update state.
Attempts:
2 left
💡 Hint
Think about complexity and how state updates depend on previous values.
ui_behavior
intermediate
2:00remaining
Effect of Context API on component re-renders
In React Native, what happens to components consuming a Context when the Context value changes?
ANo components re-render unless you manually call setState in them.
BComponents re-render only if they are wrapped in React.memo.
COnly components that use the changed part of the Context value re-render automatically.
DAll components consuming that Context re-render regardless of whether they use the changed value.
Attempts:
2 left
💡 Hint
Consider how Context propagates changes to consumers.
lifecycle
advanced
2:00remaining
State update timing with useState and useEffect
Given this React Native code snippet, what will be the console output after the button is pressed once? code: """ const [count, setCount] = useState(0); useEffect(() => { console.log('Count changed:', count); }, [count]); const onPress = () => { setCount(count + 1); setCount(count + 1); }; """
ACount changed: 1
BCount changed: 2
CCount changed: 0
DCount changed: NaN
Attempts:
2 left
💡 Hint
Remember that state updates are asynchronous and may be batched.
navigation
advanced
2:00remaining
State persistence across React Navigation screens
In React Native using React Navigation, which approach ensures state persists when navigating back and forth between screens?
AKeep state only in local component useState hooks on each screen.
BStore state in a global context or state management library like Redux or Zustand.
CReset state in useEffect cleanup when screen unmounts.
DUse React Navigation's useFocusEffect to reset state on screen focus.
Attempts:
2 left
💡 Hint
Think about where state lives when screens unmount and remount.
🔧 Debug
expert
2:00remaining
Identifying cause of stale state in React Native
Why does this React Native component always log the initial count value 0 when the button is pressed, despite updating state? code: """ const MyComponent = () => { const [count, setCount] = useState(0); const handlePress = () => { setTimeout(() => { console.log('Count is:', count); setCount(count + 1); }, 1000); }; return
AThe count state is not initialized properly with useState.
BsetTimeout delays state update causing React to ignore the setCount call.
CThe handlePress closure captures the initial count value, so setTimeout logs stale count and increments from 0 every time.
DThe Button component does not trigger onPress events correctly.
Attempts:
2 left
💡 Hint
Consider how closures capture variables in JavaScript functions.