You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I try to use Storybook in Next.js project with @storybook/nextjs, the React version which is for used rendering components in Storybook is different from Next.js.
Next.js uses their bundled React when the project is configured to use "app router", and they also let us use our own React in node_modules when the porject is configured to use "pages router".
So if I installed React 18.3.1 and Next.js 15, and also If I configured the project to use "pages router", then the React version that is used for rendering should be 18.3.1
But in @storybook/nextjs, it always force to use Next bundled React regardless of router configuration.
For Next.js 15.1.7, the version of bundled React is 19.0.0-rc-65e06cb7-20241218
This difference causes an inconsistency between Next.js rendered components and Storybook rendered components.
In my case, React 19 starts to provide ref within React props and it caused unexpected overwriting of ref pro.
I think this aliasing behavior should be optional, and ideally it should follow the Next.js configuration (if the Next.js project is configured to use pages router, than make it not to configure those aliases)
Since Next.js with pages router doesn't require any features from React canary, it is safe to use React other than bundled version.
My current workaround is adding following config to Storybook's config, main.ts
** Disclaimer** This information might be inaccurate, due to it being generated automatically
This can be fixed by modifying the webpack configuration in @storybook/nextjs. The relevant code is in code/frameworks/nextjs/src/config/webpack.ts. Add a check for the router type before applying React aliases: ts // In webpack.ts const useNextBundledReact = async (nextConfig) => { // Check if using pages router const usingPagesRouter = !nextConfig.experimental?.appDir; return !usingPagesRouter; }; export const webpack = async (config: Configuration, options: Options): Promise<Configuration> => { const shouldUseNextReact = await useNextBundledReact(options.nextConfig); if (shouldUseNextReact) { config.resolve.alias = { ...config.resolve.alias, 'react': path.dirname(require.resolve('next/package.json')), 'react-dom': path.dirname(require.resolve('next/package.json')), }; } return config; }; This will only apply the Next.js bundled React when not using the pages router.
About Greptile
This response provides a starting point for your research, not a precise solution.
Help us improve! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
Describe the bug
When I try to use Storybook in Next.js project with
@storybook/nextjs
, the React version which is for used rendering components in Storybook is different from Next.js.Next.js uses their bundled React when the project is configured to use "app router", and they also let us use our own React in node_modules when the porject is configured to use "pages router".
So if I installed React
18.3.1
and Next.js 15, and also If I configured the project to use "pages router", then the React version that is used for rendering should be18.3.1
ref: /~https://github.com/vercel/next.js/blob/48f2588b0fea2bffb5bf6534169ee112438786a6/packages/next/next-runtime.webpack-config.js#L240-L247
But in
@storybook/nextjs
, it always force to use Next bundled React regardless of router configuration.For Next.js 15.1.7, the version of bundled React is
19.0.0-rc-65e06cb7-20241218
This difference causes an inconsistency between Next.js rendered components and Storybook rendered components.
ref:
storybook/code/frameworks/nextjs/src/config/webpack.ts
Lines 34 to 48 in 8534d11
I think this aliasing behavior should be optional, and ideally it should follow the Next.js configuration (if the Next.js project is configured to use pages router, than make it not to configure those aliases)
Since Next.js with pages router doesn't require any features from React canary, it is safe to use React other than bundled version.
My current workaround is adding following config to Storybook's config,
main.ts
Reproduction link
https://stackblitz.com/edit/github-bmvmcb6b
Reproduction steps
yarn dev
to run Next.js server.yarn storybook
to run Storybook.System
Additional context
The text was updated successfully, but these errors were encountered: