KenlamCoding / loveWorking

1 stars 0 forks source link

Understand useMemo #5

Open KenlamCoding opened 1 year ago

KenlamCoding commented 1 year ago

useMemo(()=>{return xfunction()},[number] )

Why use it? the system need the performance benefits when the function you are calling is incredibly slow.

Prons: Save memory and handle preformance.

Cons: It give you performance overhead and memory overhead. It will cause additional memory usage and additional performance problems.

Call memo when

KenlamCoding commented 1 year ago

Pros 2:
Referential equality (value versus reference)

const theme=useMemo(()=>{ return { backgrondColor: dark? 'black': 'white' } },[dark])

useEffect(()=>{ console.log("Theme changed"); },[theme])

Check the reference is different or not. It is useful when React's other variable is changed but the targeted reference is the same value in the function but the useEffect is active. Using the use Memo()