Now ab hm log ye dekhte hai ki useMemp hook ka use kab karte hai
Hm React.Memo( ) ka use karte hai taki koi child element mai agar props change na ho rhe ho to wo ek se jyada baar create na ho
If we use React.Memo ( ) it work successfully but what happens when we pass an array as props
Same cheese hm yaha par deal karege but there is an case with array
Inspite of the other variabels array will be create with diffrent refrences every time the Parent component has recreated // means for child component there will be always a new props wheather it is the same array ( but with the diffrent refrence )
So we use useMemo hook to prevent the reevaluation of the child component without any change in the array ( passed in the props )
Parent components
// In the following component we are passing empty array to prevent the reevaluartion // of array multiple times with the same data // This will call only one time ( if you use this hook only inside the child not in parent // Then also your array will be recreated with new refrence as your parent component recreated // so you have to use the hook inside parent also to better array handeling <Paragraph show={true} list={useMemo(['your array content'],[]}/>
Β
Β
Β
Child components( Paragraph )
Agar hme kisi object ki kisi ek purticualr property to kisi new variable mai store karana hai to do methos hai
// You are storing the property_name of the object props in var1 and var2 const var1= props.property_name; const {property_name:var2}=props;
Here we use useMemo( ) with [list ] condition means when the list changes it is being fire the callback function and changes the list content
πAnd we know that list will never change bcz we use the usememo in the parent component to prevent to change the refrence of the array which is passing as props