javascript - Uncaught ReferenceError: React is not defined

javascript – Uncaught ReferenceError: React is not defined

javascript – Uncaught ReferenceError: React is not defined

If you are using Babel and React 17, you might need to add runtime: automatic to config.

 {
     presets: [
         @babel/preset-env,
        [@babel/preset-react, {runtime: automatic}]
     ]
 }

I was able to reproduce this error when I was using webpack to build my javascript with the following chunk in my webpack.config.json:

externals: {
    react: React
},

This above configuration tells webpack to not resolve require(react) by loading an npm module, but instead to expect a global variable (i.e. on the window object) called React. The solution is to either remove this piece of configuration (so React will be bundled with your javascript) or load the React framework externally before this file is executed (so that window.React exists).

javascript – Uncaught ReferenceError: React is not defined

I got this error because I was using

import ReactDOM from react-dom

without importing react, once I changed it to below:

import React from react;
import ReactDOM from react-dom;

The error was solved 🙂

Related posts on javascript :

Leave a Reply

Your email address will not be published.