Complete the code to import the useReducer hook from React.
import React, { [1] } from 'react';
The useReducer hook is imported from React to manage complex state logic.
Complete the code to initialize the useReducer hook with a reducer and initial state.
const [state, dispatch] = useReducer([1], { count: 0 });
The first argument to useReducer is the reducer function that updates state.
Fix the error in the reducer function to correctly update the count on 'increment' action.
function reducer(state, action) {
switch (action.type) {
case 'increment':
return { count: state.count [1] 1 };
default:
return state;
}
}The reducer should add 1 to the current count when the action type is 'increment'.
Fill both blanks to dispatch an 'increment' action when the button is pressed.
return ( <Button title="Increment" onPress={() => [1]([2])} /> );
To update state with useReducer, call dispatch with an action object having a type.
Fill all three blanks to create a reducer that handles 'increment' and 'decrement' actions.
function reducer(state, action) {
switch (action.type) {
case [1]:
return { count: state.count + 1 };
case [2]:
return { count: state.count - 1 };
default:
return [3];
}
}The reducer must match action types as strings and return the current state by default.