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

[v4]ReferenceError: React is not defined #9882

Closed
yoyo837 opened this issue Oct 24, 2020 · 3 comments · Fixed by #9885
Closed

[v4]ReferenceError: React is not defined #9882

yoyo837 opened this issue Oct 24, 2020 · 3 comments · Fixed by #9885

Comments

@yoyo837
Copy link

yoyo837 commented Oct 24, 2020

npx create-react-app my-app

then eject it, it report React is not defined

截屏2020-10-24 11 57 16

@yoyo837 yoyo837 changed the title [4.0]ReferenceError: React is not defined [v4]ReferenceError: React is not defined Oct 24, 2020
@n3tr
Copy link
Contributor

n3tr commented Oct 24, 2020

I have submitted PR #9885 to fix this.

This is because the jsx-runtime configuration gets removed webpack config during the ejection process.

In the meantime, Adding a runtime option to babel in package.json will solve the issue

// package.json
{
  "babel": {
    "presets": [
      [
        "react-app",
        {
          "runtime": "automatic"
        }
      ]
    ]
  }
}

or add presents option back to webpack.config.js (around line 409)

// webpack.config.js
{
  loader: require.resolve('babel-loader'),
  options: {
    customize: require.resolve(
      'babel-preset-react-app/webpack-overrides'
    ),
    // Add this back
    presets: [
      [
        require.resolve('babel-preset-react-app'),
        {
          runtime: hasJsxRuntime ? 'automatic' : 'classic',
        },
      ],
    ],
  }
}

@bradydowling
Copy link

Thank you @n3tr! Changing the webpack config as you suggested worked for me.

@trozler
Copy link

trozler commented Nov 12, 2020

Thanks, only package.json solution worked for me.
Edit: This didn't fully work as intended.
After trying a few different things, what worked for me was:

  1. create a new react app project.
  2. Implement @n3tr changes given by his pull request(It's only moving 2 lines of code).
  3. Finally ejecting.

I then just copied my source files over.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants