Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

with-storybook doesn't work without React import #16431

Closed
paambaati opened this issue Aug 21, 2020 · 3 comments · Fixed by #17610 or majacQ/next.js#36 · May be fixed by Jcrowell74/next.js#70, Jcrowell74/next.js#71 or Jcrowell74/next.js#73
Closed
Labels
good first issue Easy to fix issues, good for newcomers

Comments

@paambaati
Copy link
Contributor

paambaati commented Aug 21, 2020

Bug report

Next.js automatically imports React so you don't have to write import React from 'react' in every single source file. The with-storybook example works well with that import, but when it is removed, it doesn't and throws a React is not defined error.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Go to https://github.com/vercel/next.js/tree/canary/examples/with-storybook and clone the example like it says in the README.
  2. Modify the component story by removing the import React from 'react' line and run the storybook again.
  3. See error that says React is not defined.

Expected behavior

Just like the rest of Next.js, the storybook should work without having to import React explicitly.

Screenshots

N/A

System information

  • OS: macOS
  • Browser Chrome
  • Version of Next.js: 9.5.2
  • Version of Node.js: 14.8.0

Additional context

Storybook's FAQs actually documents this exact issue - see https://storybook.js.org/docs/react/workflows/faq#i-see-referenceerror-react-is-not-defined-when-using-storybooks-with-nextjs

But adding the recommended babel-plugin-react-require plugin doesn't solve the issue, as it seems to conflict (or perhaps overwrite?) with Next.js' own Babel configuration. And most discussions around this issue seem to be outdated (they don't work) with the latest v6 release of Storybook.

@timneutkens timneutkens added the good first issue Easy to fix issues, good for newcomers label Aug 21, 2020
@khattakdev
Copy link
Contributor

I'll look into this issue.

@kkfuri
Copy link

kkfuri commented Sep 8, 2020

You need to create a Next.js custom babel config with the default config so storybook uses that config.

with-storybook/.babelrc

{
  "presets": ["next/babel"],
  "plugins": []
}

references:
https://nextjs.org/docs/advanced-features/customizing-babel-config
https://storybook.js.org/docs/react/configure/babel#custom-configuration
If your project has a .babelrc file, we'll use that instead of the default config file

zerbinidamata added a commit to zerbinidamata/next.js that referenced this issue Oct 4, 2020
@kodiakhq kodiakhq bot closed this as completed in #17610 Oct 5, 2020
kodiakhq bot pushed a commit that referenced this issue Oct 5, 2020
Fixes #16431
* Added `.babelrc` with `next/babel` to remove the need of the React import on the `with-storybook` example.
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.