Posts

Showing posts from December, 2022

React Hooks - useReducer - A simple explanation (hopefully...)

Image
In this blog, I hope to provide a simple explanation of how useReducer works! Atleast, the part that I found difficult to understand. Take a goal e.g. We require a functionality, where, everytime the Increment Button is pressed, the counter should increment by 100. The reducer code to do this is written as: const  [ new State ,  dispatchAction ]  =   useReducer (   counterIncrementer , initialState ) ; This line of code lets React know that: 1. When dispatch is triggered (e.g. on click of the Increment Button), it should use the  counterIncrementer (reducer) function. 2. It should run the  counterIncrementer (reducer) function using the initialState and the action type ('increment') provided. 3. It should then return the newState as a result o f the reducer function. Here is my attempt to present this process in the form of a flowchart. Hope this helps!!