Repository navigation
[internals] Harden useDisposable React internals read with safeReact - #22986
Merged
rita-codes merged 5 commits intoJun 29, 2026
Merged
Conversation
`useDisposable` reads `__CLIENT_INTERNALS_…` / `__SECRET_INTERNALS_…` off the React namespace to detect StrictMode. The original direct read (`React.__SECRET_INTERNALS_…`) is rewritten by bundlers into a named import of `react` that React 19 doesn't export, which is the build break shipped in 9.6.0. mui#22882 mitigated it by reading through a local alias (`const X = React`). The alias works in webpack today but is the fragile form Base UI moved away from in material-ui#41190. Use the `safeReact` pattern instead: clone the namespace into a plain object (`{ ...React }`, compiled to `_extends({}, React)`). The read goes through a function-call result the bundler can't trace back to the `react` import, so it can't be rewritten into a named import on any bundler. No behavior change: missing keys are still `undefined` and fall through to a single dispose. Verified the built `.mjs` bundles clean in a webpack build against React 19, where the direct-access form errors with "`__SECRET_INTERNALS_…` is not exported from 'react'". Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Deploy previewBundle size
Check out the code infra dashboard for more information about this PR. |
Janpot
approved these changes
Jun 29, 2026
Janpot
left a comment
Member
There was a problem hiding this comment.
Small optimization could be to reuse the safeReact object @base-ui/utils is already creating.
Per review: `@base-ui/utils` already exports a `SafeReact` clone of the React
namespace, and `useDisposable` already imports from `@base-ui/utils`. Read the
internals off that shared clone instead of building a second `{ ...React }`
locally. Same bundler-safety (a property read off a named import is never
rewritten into a `react` named import); one less clone.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Member
Author
Good call, fixed! ✅ |
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.
Context
useDisposablereads__CLIENT_INTERNALS_…/__SECRET_INTERNALS_…off the React namespace to detect StrictMode (so the store survives StrictMode'smount → unmount → mountcycle with synchronous dispose).The original read was direct —
React.__SECRET_INTERNALS_…. Bundlers rewrite that into a named import ofreact, and React 19 doesn't export__SECRET_INTERNALS_…, so the build fails with:This is the error shipped in
@mui/x-internals@9.6.0. #22882 already mitigated it by reading through a local alias (const X = React). The alias works in webpack today, but it's the fragile form Base UI deliberately moved away from in material-ui#41190.Change
Read the internals off Base UI's
SafeReact— a plain clone of the React namespace that@base-ui/utilsalready exports, and whichuseDisposablealready imports from — instead of offReactdirectly. A property read on a regular named import is never rewritten into areactnamed import, so the build error can't happen on any bundler.No behavior change: missing keys are still
undefinedand fall through to a single, normal dispose.Verification
@mui/x-internals).pnpm test:unit --project x-internals), including the StrictMode double-mount and<Activity>cases..mjsin a webpack build against React 19.2.6: the original direct-access form errors with'__SECRET_INTERNALS_…' was not found in 'react', and reading the internals offSafeReactbundles clean (0 errors).