by Pawan Kumar @jsartisan
React provides the useReducer hook as an alternative to useState for managing complex state logic.
Your task is to implement your own version of useReducer from scratch.
-
Create a custom hook called
useReducerthat:- Accepts a
reducerfunction and aninitialState. - Returns the current state and a
dispatchfunction. - Updates the state when
dispatchis called with an action.
- Accepts a
-
The state update logic should follow the reducer pattern:
(state, action) => newState;
-
Ensure that the component re-renders whenever the state changes.
-
Demonstrate your custom
useReducerby using it in the given simple counter component withincrement,decrement, andresetactions.