Suppose I have a PersonCard component, which receives props, and renders a card showing the person's information.
interface PersonProps {
firstName: string,
lastName: string
}
const PersonCard: React.FC<PersonProps> = (firstName, lastName) => {
// render the things
}
Now, if I were to use React.memo to prevent unnecessary re-rendering of this component, it would look like this:
const PersonCard: React.FC(<PersonProps>) =>
React.memo(({firstName, lastName}) => {
// render the things
}
)
And this, to my understanding, would work: if my component was called twice with the same firstName and lastName, it would not re-render.
Now, my question arises when we add a reference type to the mix:
interface PersonProps {
firstName: string,
lastName: string,
hobbies: Array<string>
}
const PersonCard: React.FC(<PersonProps>) =>
React.memo(({firstName, lastName, hobbies}) => {
// render the things
}
)
In this case React.memo does a shallow comparison (as its default behavior), and will not work on the hobbies array.
Therefore, PersonCard will always re-render, even if firstName, lastName and hobbies do not change, because it will think hobbies changed:
this is effectively the same as not having React.memo at all.
So, my question is this: Am I wrong, or having any reference type passed as a prop without specifying a deep comparison callback completely nullifies the point of React.memo?