Skip to content

Latest commit

 

History

History
25 lines (15 loc) · 1.98 KB

File metadata and controls

25 lines (15 loc) · 1.98 KB

useReducer medium #react

by Pawan Kumar @jsartisan

Take the Challenge

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.

Requirements:

  1. Create a custom hook called useReducer that:

    • Accepts a reducer function and an initialState.
    • Returns the current state and a dispatch function.
    • Updates the state when dispatch is called with an action.
  2. The state update logic should follow the reducer pattern:

    (state, action) => newState;
  3. Ensure that the component re-renders whenever the state changes.

  4. Demonstrate your custom useReducer by using it in the given simple counter component with increment, decrement, and reset actions.


Back Share your Solutions Check out Solutions