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 :
- javascript – Puppeteer wait until page is completely loaded
- javascript – How to make a shared state between two react components?
- javascript – Downloading canvas image using toBlob
- javascript – Open webpage in fullscreen in Safari on iOS
- javascript – Where does TrackballControls come from?
- javascript – Local storage vs AngularJS $cacheFactory