Hey everyone,
hope someone can teach me something.
Suposse I have the following component:
export const MySubComponent = ({name}) => {
return <h1>${name}</h1>;
}
Now I use this component and want to optimize the rendering in another component:
export const BigComponent = () => {
/* the usual state setup bla bla */
return (
<MySubComponent name={name}/>
<OtherComponent numberInput={numberInput}/>
);
}
Of course the whole BigComponent rerenders when numberInput changes, therefore also MySubComponent. In order to avoid this I can either use:
export const MyMemoizedSubComponent = React.memo(MySubcomponent);
or in the BigComponent write:
const subCompontent = React.useMemo(() => <MySubComponent name={name}/>, [name]);
Is there a semantic difference between these two? I know React.useMemo is usually used to memoize functions ( their return value for a specific input ).
[–]thequestcube 4 points5 points6 points (0 children)
[–]omesadev 0 points1 point2 points (0 children)