useReducer
Explore how to use the useReducer hook in React for managing complex state logic by defining reducer and dispatch functions. Understand how to handle state updates immutably and how to use an optional init function for derived initial states. This lesson helps you implement advanced state management patterns for better component control and performance.
We'll cover the following...
Managing complex states
The useReducer() Hook is an alternative solution for useState() and allows us to manage more complex states. It’s based on flux architecture in which a reducer function creates a new state by being passed the last state and an action.
The reducer function is called by executing a dispatch function, that in turn receives an action. The action is an object which always has a type property and often a payload property
attached. From this action and the previous state, the reducer function can then create the new state. We can summarize this in the following form:
(oldState, action) => newState
...