React JS UseReducer

  • useReducer is a React Hook that lets you manage state in a functional component. It’s similar to useState but with additional features for managing complex state transitions and updates.
  • useReducer takes two arguments: the first is a reducer function, and the second is the initial state. The reducer function takes the current state and an action as arguments and returns a new state. The action is an object that describes an action that should be performed on the state.

Code example:

In this above example, the useReducer Hook returns an array with two values: state and dispatch, where state is the current state of the component & the dispatch function is used to update the state by sending an action to the reducer function. When you call dispatch with an action, the reducer function is called with the current state and the action, and it returns the new state.

Output:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top