0
0
React Nativemobile~20 mins

useReducer hook in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
useReducer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this counter component using useReducer?
Consider this React Native component using useReducer to manage a counter. What will the displayed number be after pressing the 'Increment' button twice?
React Native
import React, { useReducer } from 'react';
import { View, Text, Button } from 'react-native';

const reducer = (state, action) => {
  switch(action.type) {
    case 'increment':
      return { count: state.count + 1 };
    case 'decrement':
      return { count: state.count - 1 };
    default:
      return state;
  }
};

export default function Counter() {
  const [state, dispatch] = useReducer(reducer, { count: 0 });

  return (
    <View>
      <Text>{state.count}</Text>
      <Button title="Increment" onPress={() => dispatch({ type: 'increment' })} />
    </View>
  );
}
A2
B1
C0
DError: dispatch not defined
Attempts:
2 left
💡 Hint
Each press of the 'Increment' button sends an 'increment' action to the reducer which adds 1 to count.
lifecycle
intermediate
1:30remaining
When does the reducer function run in a useReducer hook?
In React Native, when is the reducer function passed to useReducer called?
AEvery time the component re-renders
BOnly once when the component mounts
COnly when the dispatch function is called with an action
DWhen the component unmounts
Attempts:
2 left
💡 Hint
Think about what triggers state changes in useReducer.
📝 Syntax
advanced
2:00remaining
What error does this useReducer code produce?
Identify the error in this React Native useReducer snippet:
React Native
function reducer(state, action) {
  if(action.type === 'increment') {
    state.count += 1;
    return state;
  }
  return state;
}

const [state, dispatch] = useReducer(reducer, { count: 0 });
ABug: State is mutated directly, causing unexpected behavior
BError: Cannot assign to read only property 'count'
CNo error, works correctly
DSyntaxError: Missing return statement
Attempts:
2 left
💡 Hint
Reducers should never change the existing state object directly.
navigation
advanced
2:30remaining
How to reset state with useReducer on screen focus in React Native?
You want to reset your useReducer state to initial when the screen gains focus in React Navigation. Which approach works best?
AUse useState instead of useReducer for easier reset
BCall dispatch({ type: 'reset' }) inside useEffect with empty dependency array
CReset state by re-mounting the component manually
DCall dispatch({ type: 'reset' }) inside a useFocusEffect hook
Attempts:
2 left
💡 Hint
Screen focus events are best handled with useFocusEffect from React Navigation.
🧠 Conceptual
expert
2:00remaining
Why prefer useReducer over useState for complex state in React Native?
Which reason best explains why useReducer is preferred over useState for managing complex state logic?
AuseReducer is faster than useState in all cases
BuseReducer allows grouping related state updates in one place, improving maintainability
CuseReducer automatically batches state updates without extra code
DuseReducer requires less code to update simple state values
Attempts:
2 left
💡 Hint
Think about how state logic complexity affects code clarity.