Closed Pernick closed 2 years ago
Issue #5642 describing similar problem mentions updating to 1.0.9
helps. Sadly I can not confirm this - updated reproduction repo as well.
I'm getting Uncaught ReferenceError: React is not defined.
error using material-ui pickers in production mode, I've created a minimal repo
I've tested both 1.0.7
and 1.0.9
version mentioned in #5642 and it did NOT solve the problem,is there any solution?
Even with newer 1.1.0
I have not had any luck. Will report back if I discover something. This issue prevails in every one of my apps when using react-custom-scrollbars
. The simplest solution for me is not to use it. That probably wouldn't work for you.
No it would not! I'm trying anything that comes in my mind, ranged from upgrading material-ui-picker library to using rollup or commonJs options to transform the code, or suppress the issue. So far nothing worked.
It worked at last!!
I honestly don't know which step made it work:
1- first I removed react plugin
from @vitejs/plugin-react
in production mode :
plugins: mode === "development" ? [react({}), ...pluginsArray] : pluginsArray,
2- then I added an alias for @material-ui/pickers
to use esm :
{
find: /^@material-ui\/pickers\/(.*)/,
replacement: "@material-ui/pickers/esm/$1",
},
3- then added jsxInject: "import * as React from 'react'",
to esbuild option,
4- and finally added globals to build => rollupOptions => output => globals :
globals: {
react: "React",
"react-dom": "ReactDOM",
},
and the error is gone!
Confirmed same issue ("React is not defined") for @material-ui/pickers using plugin-react 1.1.0 in production builds.
@nabc's solution worked for me! What worked here was the 2nd step
Can confirm that the issue still persists on 1.1.3
, step 2 of @nabc's solution doesn't appear to work for me 😭 . The only thing that worked was disabling the react plugin when in production mode as in step 1 of @nabc's solution.
edit: we can also just pass jsxRuntime: 'classic'
to the react plugin options as well https://github.com/vitejs/vite/blob/8bdb1848dd777f8de9f63b27a7746cec65db5b70/packages/plugin-react/src/index.ts#L24-L28
I tried the react-custom-scrollbars
with jsxRuntime: 'classic'
and the other proposed solutions, none of them seems to work for me.
But as most people here resolved their problem with one of the provided solutions and I replaced the react-custom-scrollbars
package in my apps, I am closing this issue.
Describe the bug
When using @vitejs/react-plugin with
react-custom-scrollbars-2
(but other packages have similar issues - can be found here: https://github.com/alloc/vite-react-jsx/issues/10), reference to react is not transformed in all locations the same way.react-custom-scrollbars-2
imports react utility functions:import { Component, createElement, cloneElement } from 'react';
https://github.com/RobPethick/react-custom-scrollbars-2/blob/master/src/Scrollbars/index.js#L3In the library output, it looks like this (emphasis on the
_react.
:But after vite build it is changed to:
Every occurence but one is using the
_react3
variable, onlyReact.createElement
is remaining - which leads toUncaught ReferenceError: React is not defined
.Reproduction
yarn
yarn run build
yarn run serve
This does not happen in dev mode. The test repo is using manual chunks, but I have not found any important difference when using them differently/not using them at all.
System Info
Used Package Manager
yarn
Logs
No response
Validations