Skip to content
This repository has been archived by the owner on Aug 13, 2023. It is now read-only.

Media Player - Limit Unnecessary re-renders #3238

Merged
merged 8 commits into from
Mar 13, 2020

Conversation

ryanmccombe
Copy link
Contributor

@ryanmccombe ryanmccombe commented Mar 10, 2020

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 object

Code changes:

  • Memoise the component based on a deep equality check

  • I have assigned myself to this PR and the corresponding issues
  • Automated jest tests added (for new features) or updated (for existing features)
  • This PR requires manual testing

@ryanmccombe ryanmccombe changed the title Prevent unnecessary re-renders Media Player - Prevent unnecessary re-renders Mar 10, 2020
@ryanmccombe ryanmccombe self-assigned this Mar 10, 2020
@ryanmccombe ryanmccombe changed the title Media Player - Prevent unnecessary re-renders Media Player - Limit Unnecessary re-renders Mar 11, 2020
@ryanmccombe ryanmccombe marked this pull request as ready for review March 11, 2020 12:10
// 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);

Copy link
Contributor

@RichardPK RichardPK Mar 11, 2020

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?

Copy link
Contributor Author

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

Screen Shot 2020-03-11 at 17 49 21

Copy link
Contributor Author

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 🤣

Copy link
Contributor

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.

Copy link
Contributor

@RichardPK RichardPK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢

@ryanmccombe ryanmccombe merged commit e79782a into latest Mar 13, 2020
@amywalkerdev amywalkerdev deleted the media-player-prevent-rerendering branch March 16, 2020 16:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Media Player reloads when using skip to content
4 participants