@react-aria/toast causing Dynamic require of "react" is not supported
๐ Bug Report
When instantiating a new ToastQueue, or using the useToastState hook, I get a runtime error Dynamic require of "react" is not supported.
๐ค Expected Behavior
There should be no runtime error when using Toasts.
๐ฏ Current Behavior
๐ Possible Solution
I tried to externalize react, which initially removed the Dynamic require error, but it led to having 2 versions of react in the DOM.
๐ฆ Context
We are trying to make use of @react-aria/toast to handle Toasts for our design system.
I feel as if this could be related to using esbuild/tsup? Here is our config if that may alude to anything:
export default defineConfig({
outDir: "dist",
format: ["esm", "cjs"],
dts: true,
platform: "browser",
external: ["react"],
clean: true,
splitting: true,
treeshake: true,
entry: ["src/**/*.ts"],
esbuildPlugins: [svgrPlugin(), dynamicImport()],
});
๐ป Code Sample
This is enough to cause the runtime error.
const toastQueue = new ToastQueue<ReactNode>({
maxVisibleToasts: 5,
});
Another way is:
const state = useToastState<ReactNode>({
maxVisibleToasts: 5,
});
๐ Your Environment
| Software | Version(s) |
|---|---|
| react-aria | 3.26.0 |
| react-stately | 3.24.0 |
| Browser | Firefox Dev 116 |
| Operating System | Windows 10 |
| @react-aria/toast | 3.0.0-beta.2 |
๐งข Your Company/Team
Lightspeed HQ
๐ท Tracking Issue (optional)
n/a
I don't think anything in our code is doing a dynamic require, but maybe related to the external: ["react"] in your config? Not really familiar with esbuild though.
Appreciate you getting back! I haven't had a chance to debug further :( Marking react as external here means that esbuild assumes the module is available at runtime. So react doesn't need to be bundled with our package. This is why without external, our tests resulted in 2 versions of React in the dom.
I'd be happy to try any ideas, otherwise I think its safe to call this a build tooling issue and you can close ๐
Appreciate you getting back! I haven't had a chance to debug further :( Marking react as external here means that esbuild assumes the module is available at runtime. So react doesn't need to be bundled with our package. This is why without external, our tests resulted in 2 versions of React in the dom.
I'd be happy to try any ideas, otherwise I think its safe to call this a build tooling issue and you can close ๐
@kysley Did you ever figure out a solution for this? It seems like these components are meant for building component libraries, but they do not seem to be compatible with bundling tools like esbuild or tsup. I am finding it very difficult to build a reusable component.