Skip to main content
InterviewHubPRO
Section Overview
Q276

Question 276 of 407

Part 9: React Hooks Deep Dive (262 - 316)

68%
React Core & Hooks (407 Qs)•Part 9: React Hooks Deep Dive (262 - 316)

What is useReducer? Why do you use useReducer?

junior LevelCode ExampleHooks

The useReducer hook is a React hook used to manage complex state logic inside functional components. It is conceptually similar to Redux. i.e, Instead of directly updating state like with useState, you dispatch an action to a reducer function, and the reducer returns the new state.

The useReducer hook takes three arguments:

js
 const [state, dispatch] = useReducer(reducer, initialState, initFunction);
  • **reducer**: A function (state, action) => newState that handles how state should change based on the action.
  • **initialState**: The starting state.
  • **dispatch**: A function you call to trigger an update by passing an action.

The useReducer hook is used when:

  • The state is complex, such as nested structures or multiple related values.
  • State updates depend on the previous state and logic.
  • You want to separate state update logic from UI code to make it cleaner and testable.
  • You’re managing features like:
  • Forms
  • Wi