React
Life Cycle

Re-render

There is element in the index.html which id is root and render the react dom element
The react dom object itself is a virtual dom which is lightweight, which looks like:
For every state change, the page will be re-rendered again, the state , variable , function, useEffect in the render scope will be independent to previous one
For each rendering, the functional component will be returned and then triggered useEffect
The virtual dom will be saved in memory and will be compared with the previous virtual dom with its internal differing algorithm, and just update the changed part on the real dom which is into dom which id is root so as to increase the efficiency

Hook
useState
When you call
setState, React schedules the state update and marks the component for a re-render. This scheduling is asynchronousReact batched state updates in event handlers to reduce the number of re-rendering
useEffect
It is used to listen the dependencies change, e.g: state, and execute the side effect, which is equal to lifecycle event of class component
When the component is mounted, the side effect will be triggered once first.
When component is unmounted, the return function will be triggered and based on the previous useEffect. After the new user interface finish re-rendering, , and finally trigger the new useEffect function
The function of useEffect will always be triggered in every re-rendering as there is no dependency array
The function of useEffect will only be triggered once as the dependency array is empty, there will not have any dependency change to trigger useEffect
The function of useEffect will be triggered due to the change of the state
Case Study 1
As the state and variable in render scope is put in the useEffect, it is not recommended to put an empty array as it may cause potential bug
Case Study 2
As the function in render scope is put in useEffect, it is recommended to put the function in the dependency array
As we do not want the useEffect always be triggered as the function is always be changed in every re-rendering, so we use useCallback to save the function except the dependency is changed
Case Study 3
As async function have the delay, we cannot make sure of order of the setState. In order to prevent from set the wrong data which is based on the the value or state in previous rendering, it is recommended to make return function to stop setting wrong value after the state is updated
UseContext
Share all the data from component with upper layer to components with lower layer without passing props
useReducer
It is an advanced version of useState, since the setting function of the useState is only allow one situation and one return value. But useReducer can return different base on the the type, it is more suitable to handle more complicated logic
useContext/useReducer vs Redux
useContext/useReducer : it is easier to setup and more suitable to put variables with low-frequency update on it , since every time the variable passed by useContext, the area inside the provider will be re-rendered
Redux: it is more complicated to setup, and middleware is allowed to use to handle async function. each component will subscribe (add listener) to their connected state. If the correspondent state is changed, the listener will be triggered, so that only that component will be re-rendered but not whole of the area inside the provider
useCallback
When the component is re-rendered, the functions will also be re-rendered. The purpose of useCallback is return the memorized callback function until the one of the dependency is changed.
useMemo
When the component is re - rendered, the variable will also be re-rendered. The purpose of useMemo is return the memorized value until the one of the dependency is changed.
useMemo vs useCallback
The effect of useMemo can equal to useCallback in this case
The drawback of abusing useMemo/ useCallback
The time of the first time re-rendering will be much larger as it memorizes the value/function
The ram consumed in the browser will be much larger
The condition of using useMemo/ useCallback
The variable require long time to be rendered , for example : have a larger loop and return the result
The function is called in useEffect
useRef
The current property will be initialized by passing the argument , and if the current is changed, it will be saved, but the component will not be re-rendered, it can be used to store the value which is not related to the ui
The reference object can be acted as the ref of the element, so that the current property will be corresponded to the html element. We can make use good use of the reference to manipulate the element, such as focus
useImperativeHandle
To lets you customize the handle exposed as a ref
Mainly for exposing the child state, method to the parent or your own method
useLayoutEffect
useLayoutEffectis a version ofuseEffectthat fires before the browser repaints the screen, to enhance the user experience and the shift of component
The code inside
useLayoutEffectand all state updates scheduled from it block the browser from repainting the screen. When used excessively, this makes your app slow. When possible, preferuseEffect.
useTransition
The function you pass to
startTransitionmust be synchronous. React immediately executes this function, marking all state updates that happen while it executes as Transitions.The hook can be used prioritize the state update . A state update marked as a Transition will be interrupted by other state updates and marked as lower priority
If there are multiple ongoing Transitions, React currently batches them together.
The state update inside the startTransition will be non-blocking, which means that the state update will not be blocked by heavy computation and be freeze, it will interrupt the slow rendering immediately
useDeferredValue
The deferred value is considered as a low priority, its deferred “background” rendering is interruptible even if the deferred value is updated.
For example, if you type into the input again, React will abandon it and restart with the new value. Also, network request is pending, the rendering is also be suspended
useDeferredValuelets you prioritize updating the input (which must be fast) over updating the result list (which is allowed to be slower)
useOptimistic (React 19)
It provides a function that takes the current state and the input to the action, and returns the optimistic state to be used while the action is pending.
The state is called the “optimistic” state because it is usually used to immediately present the user with the result of performing an action, even though the action actually takes time to complete.
useActionState (React 19)
It pass
useActionStatean existing form action function as well as an initial state, and it returns a new action that you use in your form, along with the latest form state and pending state
Custom Hook
Can do a custom hook and apply to different component instead of making high order component

Last updated
Was this helpful?
