react icon indicating copy to clipboard operation
react copied to clipboard

Throw if React and React DOM versions don't match

Open acdlite opened this issue 1 year ago • 8 comments

Throw an error during module initialization if the version of the "react-dom" package does not match the version of "react".

We used to be more relaxed about this, because the "react" package changed so infrequently. However, we now have many more features that rely on an internal protocol between the two packages, including Hooks, Float, and the compiler runtime. So it's important that both packages are versioned in lockstep.

Before this change, a version mismatch would often result in a cryptic internal error with no indication of the root cause.

Instead, we will now compare the versions during module initialization and immediately throw an error to catch mistakes as early as possible and provide a clear error message.

acdlite avatar May 23 '24 16:05 acdlite

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 28, 2024 5:59pm

vercel[bot] avatar May 23 '24 16:05 vercel[bot]

Comparing: 4ec6a6f71475a6f2fee39a0e604ddbbd2f124164...38bc3c5062f76039a66781ec9df93dd0ee9ab6a8

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.66 kB 6.66 kB +0.05% 1.82 kB 1.82 kB
oss-stable/react-dom/cjs/react-dom-client.production.js +0.06% 496.04 kB 496.36 kB +0.07% 88.77 kB 88.83 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.67 kB 6.67 kB = 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js +0.07% 500.84 kB 501.18 kB +0.08% 89.46 kB 89.52 kB
facebook-www/ReactDOM-prod.classic.js +0.05% 593.48 kB 593.80 kB +0.06% 104.38 kB 104.44 kB
facebook-www/ReactDOM-prod.modern.js +0.05% 569.87 kB 570.18 kB +0.08% 100.77 kB 100.85 kB
test_utils/ReactAllWarnings.js Deleted 63.82 kB 0.00 kB Deleted 15.95 kB 0.00 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-experimental/react-dom/cjs/react-dom-server.node.production.js +0.25% 237.81 kB 238.41 kB +0.49% 42.61 kB 42.82 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.production.js +0.25% 241.44 kB 242.04 kB +0.47% 43.26 kB 43.46 kB
oss-stable/react-dom/cjs/react-dom-server.bun.production.js +0.24% 200.08 kB 200.57 kB +0.58% 37.21 kB 37.42 kB
oss-experimental/react-dom/cjs/react-dom-server.bun.production.js +0.24% 216.21 kB 216.72 kB +0.50% 39.35 kB 39.54 kB
oss-stable/react-dom/cjs/react-dom-server.node.production.js +0.23% 212.73 kB 213.22 kB +0.52% 39.05 kB 39.26 kB
oss-stable/react-dom/cjs/react-dom-server.edge.production.js +0.23% 216.52 kB 217.01 kB +0.48% 40.31 kB 40.51 kB
oss-stable-semver/react-dom/cjs/react-dom-server.bun.production.js +0.22% 200.06 kB 200.50 kB +0.58% 37.19 kB 37.40 kB
oss-stable-semver/react-dom/cjs/react-dom-server.node.production.js +0.21% 212.70 kB 213.15 kB +0.52% 39.03 kB 39.23 kB
oss-stable-semver/react-dom/cjs/react-dom-server.edge.production.js +0.21% 216.50 kB 216.94 kB +0.48% 40.28 kB 40.48 kB
test_utils/ReactAllWarnings.js Deleted 63.82 kB 0.00 kB Deleted 15.95 kB 0.00 kB

Generated by :no_entry_sign: dangerJS against 38bc3c5062f76039a66781ec9df93dd0ee9ab6a8

react-sizebot avatar May 23 '24 17:05 react-sizebot

react-is is another candidate for this. ReactIs.isElement will also not work if you use React 19 with an earlier react-is (like recharts did) or vice versa. The fix is usually to move react-is to peer dependencies so that the app can ensure the version matches.

eps1lon avatar May 24 '24 17:05 eps1lon

Weren't we going to delete react-is entirely though? :D

acdlite avatar May 24 '24 19:05 acdlite

Some design system libraries need at least support for isElement (can be moved to react) and isValidElementType (should be moved to renderers). You oftentimes find APIs were either element types or elements are expected in a prop and these utils help identify what you actually received. Though isElement only makes sense in the context of element introspection (e.g. element.props) or cloneElement. If we remove those APIs, I can't think of use cases for isElement apart from runtime type checking. Still leaves isValidElementType.

eps1lon avatar May 24 '24 19:05 eps1lon

React has isValidElement already? https://react.dev/reference/react/isValidElement

rickhanlonii avatar May 24 '24 19:05 rickhanlonii

Another prominent use case is Jest snapshot testing, as reported in https://github.com/facebook/react/pull/28813#issuecomment-2077326950. A version mismatch might break those tests because they rely on react-is.

unstubbable avatar May 24 '24 20:05 unstubbable

Another prominent use case is Jest snapshot testing, as reported in https://github.com/facebook/react/pull/28813#issuecomment-2077326950. A version mismatch might break those because they rely on react-is.

I think what pretty-format needs is an API to get the display name not necessarily an API to change control flow based of the element kind. So for them it seems sufficient if we'd start implementing .displayName on every element type.

eps1lon avatar May 24 '24 20:05 eps1lon