-
Notifications
You must be signed in to change notification settings - Fork 54
Media Player - Limit Unnecessary re-renders #3238
Conversation
// re-renders when the object reference changes, but the content is the same. | ||
// We only rerender if the prevProps and nextProps fail deep equality check | ||
export const CanonicalMediaPlayer = memo(CanonicalMediaPlayerComponent, equals); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be unnecessarily explicit, as I'm describing essentially what ramda equals does implicitly, but, for future readability - I'm not sure it's clear that equals is being passed prevProps and nextProps?
I quite like the model of passing a function isEqual
as the second argument in the memo and then declaring the function along these lines: (though I realise === may always evaluate to false even if the objects are principally 'the same', so may need rearranging)
const isEqual = (prevProps, nextProps) => {
return prevProps === nextProps
}
I think this makes it clearer what's going on and maybe negates the need for adding comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cheers 👍 added wrapping function
we still need the deep equality check as ===
just checks if the objects are the same instance, ie, the same place in memory
the bug we're dealing with is parent components creating an entirely new object and passing it down - but that object has all the same values as the previous one
different objects will fail the ===
check but, if they have the same content, they'll pass the ramda equals
check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI is failing because this wrapping function doesn't have test coverage 🤣
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-0.1% you naughty boy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚢
Resolves bbc/simorgh#5528
Overall change:
Limit unnecessary re-rendering of the component, to prevent the iframe from resetting
This was due to upstream components creating new
mediaInfo
objects that contain the same content as the previous objectCode changes: