fix(react): memoize Provider context value#1787
Merged
Merged
Conversation
The `Provider` returned by `createPlayer()` was building a fresh context value object on every render. Every `useContext(PlayerContext)` consumer therefore re-rendered on any parent re-render — even when none of the value's members changed. Combined with callback-ref usage in media components (`HlsVideo`, `DashVideo`, etc.), this caused the underlying media to detach and re-attach on every parent state change. The most visible symptom: `onEnded` fires, parent state updates, the video rewinds to t=0 and plays again. Wrap the value object in `useMemo` keyed on `[store, media, container, popupGroup]`. `setMedia` and `setContainer` are `useState` setters and are identity-stable; `store` and `popupGroup` are also initialized once via `useState(() => ...)` and never change for the lifetime of the Provider, but they're listed in the dep array to satisfy Biome's `useExhaustiveDependencies`. Adds a regression test that asserts context value identity is preserved across forced parent re-renders. Signed-off-by: James <james@BlainWebDesign.com>
✅ Deploy Preview for vjs10-site ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
📦 Bundle Size Report🎨 @videojs/html — no changesPresets (7)
Media (10)
Players (5)
Skins (30)
UI Components (39)
Sizes are marginal over the root entry point. ⚛️ @videojs/react — no changesPresets (7)
Media (9)
Skins (27)
UI Components (33)
Sizes are marginal over the root entry point. 🧩 @videojs/core — no changesEntries (14)
🏷️ @videojs/element — no changesEntries (2)
📦 @videojs/store — no changesEntries (3)
🔧 @videojs/utils — no changesEntries (10)
📦 @videojs/spf — no changesEntries (4)
ℹ️ How to interpretJS sizes are initial static graph totals (minified + brotli). Lazy dynamic chunks are shown separately when present.
Run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces #1541 because the source fork has maintainer edits disabled, so the review cleanup could not be pushed there.
Related to #1296.
What changed
createPlayer().Providercontext value so parent re-renders do not rebuild the value object unnecessarily.Verification
pnpm --filter @videojs/react... buildpnpm -F @videojs/react test src/player/tests/create-player.test.tsxpnpm lint:fix:file packages/react/src/player/create-player.tsx packages/react/src/player/tests/create-player.test.tsxNote
Low Risk
Small React Provider change with a focused regression test; no auth, data, or API surface changes.
Overview
Fixes unnecessary context updates when the parent re-renders but player state is unchanged (#1296).
The
createPlayer().Providernow builds its context value withuseMemo, keyed onstore,media,container, andpopupGroup, instead of allocating a new object every render. Consumers that depend on context identity or shallow stability should no longer re-render solely because an ancestor updated.A regression test forces several parent re-renders and asserts the context value reference stays the same.
Reviewed by Cursor Bugbot for commit bf40bb2. Bugbot is set up for automated code reviews on this repo. Configure here.