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

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 [newStatedispatchAction= 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 of the reducer function.

Here is my attempt to present this process in the form of a flowchart.




Hope this helps!!
 

Comments

Popular posts from this blog

Reading a pre-populated sqlite database using react native with expo..

React Hooks - useState Vs useRef (An explanation)