loading

useReducer


The useReducer Hook is analogous to the UseState Hook.

It supports custom state logic.

If you need to maintain track of numerous pieces of state that rely on sophisticated logic, useReducer could be handy.


Syntax

The useReducer hook takes two arguments.

UseReducer (<reducer>, <initialState>)

The reducer function contains your custom state logic, and initialState can be a simple value or an object.

The useReducer Hook returns both the current state and the dispatch method.

Here’s an example of useReducer in a counter app:

Example:

----- Example mukavu -----

This is simply the logic for keeping track of todo completion status.

The logic for adding, deleting, and completing a todo might be contained within a single useReducer the number of actions in Reducer Hook.

Share this Doc

useReducer

Or copy link

Explore Topic