Use Reducer
Use Reducer
Parameters
In the above example, dispatch is the function we’ll use to change count.
dispatch function
type Payload
• type is like the name of the action. Optional
• It tells the reducer what kind of update • payload is the extra information sent
you want to make. with the action.
• Think of it as a label or instruction like: •We use it when the reducer needs
"increase", "decrease", "reset", etc. some data to update the state.
• That action tells the reducer function what to do (like increase, decrease, etc).
• It does not return anything but causes the state to update and the component to
re-render with the new state.
Example
Let’s go to our page and first click on increment and then set to 100
Dispatching: increment
Reducer CALLED
Previous state: { count: 0 }
Action received: { type: 'increment’ }
New state after action: { count: 1 }