Benison
Benison

Reputation: 167

Are child components of a memoized component memoized too?

Does the child component returned from useMemo or wrapped in React.memo() are memoized too? (Apologies, this might be a silly question)

For example:

React.memo(<Parent><Child/></Parent>);

Here the Parent is memoized, is the Child too memoized?

Upvotes: 2

Views: 1402

Answers (1)

Giorgi Moniava
Giorgi Moniava

Reputation: 28684

The child isn't memoized in the sense that React doesn't apply React.memo to the child too.

But the child is memoized in the following sense: Let's say component A is wrapped in React.memo and its child component B not. If A is not re rendered because of memoization then B will not re render too.

Also from the docs:

React.memo only checks for prop changes. If your function component wrapped in React.memo has a useState or useContext Hook in its implementation, it will still rerender when state or context change.

So if components state gets modified, then it will be re rendered, despite if it or parent is memoized.

Also if A is re rendered because something in its state changed, then its child B will re render too (assuming B is not memoized).

Upvotes: 5

Related Questions