React only cares about components (+ props, state, context, etc..) for building user interfaces.
ReactDOM is interface to the web and RealDom is what user sees.
React determines how the component tree currently looks like and what is should look like. ReactDOM recieves the differences and then manipulates the real DOM.
Re-evaluating state, context... !== Re-rendering the DOM
Whenever something which has already defined specifically changed, React re-executes component functions. With this change, RealDOM only changes the differences by react.
Child components depends on change of parent components.
Because of this, even if there isn't any changes in child components, they will be executed when parent component re-evaluated or re-executed.
Actually it is useless thing to occur execution even though child components don't have any changes.
export default React.memo(component);
For example,
const click = useCallback(() => {
setShow((prevBool) => {
return !prevBool;
});
}, []);
If we know that clikc func never be changed, we can use 'useCallback' and prevent recreating that function. 'useCallback' also has dependencies which is same thing of useEffect().(Don't forget that state function already defined as unchangable thing by react.)
Once we defined funcion with useCallback(), it will not recreated later.
If we use some values in that function and want to recreate that function as to changes of variables, we need dependencies of them.
const click = useCallback(() => {
if(condition) {setShow((prevBool) => {
return !prevBool;
});}
}, [condition]);
'condition' is variable, and if it changed, we will recreate clikc function.
State is comming from react and React manage that to connect your component.
After app run at the first time, react will not re-create state again.
That's why we don't need to care about whethere state may be re-initialized.
setState('something') -> Schedules a state update with 'something' -> Scheduled state change (This is similar concept with async). -> new state 'something' -> re-evaluate component.
This is why it is recommended to use callback form when your state change depends on previous state(safer way).
When you have multiple state changes in a function, react will not execute each of them. After the function call finished, the state changes will re-rendered at once.(This is batching and it means there is just one scheduling even though there are multiple state changes in function).
We have some logics in our code sometimes like sorting array , mapping array ...
If it treats huge data, it might occur loading of your code process.
So we need to cut down that unneccesary process with useMemo.
Its usage is almost same with useCallback.
Just wrapping a variable which doesn't need to be changed whenever function re-rendered or a logic which only need to be executed when some values changed in that logic function with useMemo
useMemo(() => {} , [dependecies])
All above things is for optimization, so there is no need to care about these problems at the first time