Core Concept
Imagine your app’s state is described as a plain object.
For example, the state of a todo app might look like this:
This object is like a “model” except that there are no setters. This is so that different parts of the code can’t change the state arbitrarily, causing hard-to-reproduce bugs.
To change something in the state, you need to dispatch an action.
An action is a plain JavaScript object (notice how we don’t introduce any magic?) that describes what happened. Here are a few example actions:
Enforcing that every change is described as an action lets us have a clear understanding of what’s going on in the app. If something changed, we know why it changed. Actions are like breadcrumbs of what has happened.
Finally, to tie state and actions together, we write a function called a reducer. Again, nothing magical about it—it’s just a function that takes state and action as arguments, and returns the next state of the app. It would be hard to write such a function for a big app, so we write smaller functions managing parts of the state:
And we write another reducer that manages the complete state of our app by calling those two reducers for the corresponding state keys:
useState
Manage state for Functional Component
States in FC can be an object, OR number, string, anything in CC, state must be an object useStage always exports 2 things: states, stateAction use Array destructuring to get those 2 exported things
With React Hook, React NOW WANT you to use useStage multiple times to track multiple state values
What if we use an object to manage multiple state values?
When we change state values, we have to declare the WHOLE OBJECT {count: ...,name:..., otherState:...} When we call any value, we have to provide object name state.things (longer code) Even though you can overide with ...state A full explaination here:
Lifting State
Xem video của thầy của Vinh để hiểu hơn
useEffect
Manage Lifecycle for FC
Similar to:
useEffect run everytimes a Component has changed (useState)
run 1st times = componentDidMount run every change state = componentDidUpdate useEffect to sync state, props within components
update Page Title, User Counts...
useEffect with Condition / Dependencies
Add 2nd call to useEffect
unlike CC, all methods must go to under 1 componentDidMount or DidUpdate
Clean-up useEffect
componentWillUnmount
to de-register addEventListener when a button disappear useReducer
A reducer contains 2 things: state, and action
A state change is defined by an action A reducer function connect state and action Use dispatch to run defined functions in reducer
If EASY STATE - useState
If more COMPLEX STATE - useReducer
remove logic from components, store somewhere upper 1. Create a reducer (state + action)
2. Import and Use
useContext
Passing props around is the hardest thing to do.
An example is here:
Read more about Context:
useContext is a way to pass data through component tree 1. Create Context
2. Pass Context to Children in JSX
Wrap around return JSX with Context Pass values in Context Provider All children and its grand-children will be able to receive these values Remove passing values in Components declaration
3. Receive passing Value at Children
remove passed Props (no passing through component anymore) receive passed values through destructuring Context