DEV Community

Cover image for useReducer in React?
Arul .A
Arul .A

Posted on

useReducer in React?

  • React provides several hooks to manage in functional components.One of the mostly used hook in react.

  • It is used in mostly when the state management is complex.

What is useReducer?

  • Usereducer is a react hook that manages state using reducer function .Instead of update state directly,using dispatch action describe what should happen ,and the reducer decides how the state changes.

Syntax :

const [state, dispatch] = useReducer(reducer, initialState);
Enter fullscreen mode Exit fullscreen mode
  • state – Current state value.
  • dispatch – Function used to send actions.
  • reducer – Function that updates the state.
  • initialState – Initial value of the state.

Why use useReducer?

  • While useState is perfect for simple state updates, useReducer is better when:

1.State contains multiple values.
2.State update logic is complex.
3.Multiple actions affect the same state.
4.You want cleaner and more organized code.

Top comments (0)