useReducer Flashcards
React hook
What is useReducer?
useReducer is a React Hook that lets you add a “reducer” to your component.
const [state, dispatch] = useReducer(reducer, initialArg, init?)
Why use useReducer?
When use useReducer?
Where use useReducer?
How do useReducer work?
What is “reducer” in component concept?
How many parameters in useReducer?
Three parameters:
1/ reducer()
2/ initialArg
3/ init?
What is reducer() parameter?
The reducer function that specifies how the state gets updated.
The reducer() take state and action as arguments, and reducer() should return the next state.
What does useReducer return?
useReducer return an array has two values:
1/ The current state. During the first render, it’s set to init(initialArg) or initialArg (if there’s no init). 2/ The dispatch function that lets you update the state to a different value and trigger a re-render
What is “dispatch function”?
The “dispatch function” is returned by useReducer.
The “dispatch function” lets you update the state and trigger a re-render.
Need to pass the action as only argument to the dispatch function <=> The action is initiated through dispatch func.
What is the “action”?
The “action” is performed by user (pass through dispatch function).
An action is usually an object with a “type” property identifying it and other properties (optional).
Why does “dispatch function” exist?
I think the “action” is only passed to reducer() by dispatch function. And that is a reason =)))