The error message:
Uncaught ReferenceError: React is not defined
at ./src/app/routing/index.jsx (index.jsx:7:14)
at __webpack_require__
indicates that the React variable is not recognized in your index.jsx file. This typically happens because React is not imported, but JSX syntax is being used, which requires React to be in scope.
Why This Happens
When you use JSX (e.g., <MainLayout />, <>page</>), Babel (or your JSX transpiler) transforms this syntax into React.createElement calls. For this transformation to work, React must be available in the scope of the file where JSX is used.
In React versions prior to 17:
- Mandatory Import: You must import React in any file that uses JSX.
In React 17 and later:
- New JSX Transform: React introduced a new JSX transform that doesn't require you to import React explicitly when using JSX. However, this requires your project to be configured to use the new transform (e.g., updated Babel or TypeScript settings).
Given the error you're encountering, it's likely that either:
- You're using a React version prior to 17, or
- Your project is not configured to use the new JSX transform.
Solution
To resolve the error, you should import React at the top of your index.jsx file. This ensures that React is defined and available for JSX transformations.
Here’s how you can modify your index.jsx:
// Import React
import React from 'react';
import { createBrowserRouter } from 'react-router-dom';
import MainLayout from '@/shared/ui/main-layout/index.jsx';
export const router = createBrowserRouter([
{
path: '/',
element: <MainLayout />,
children: [
{
index: true,
element: <>page</>,
},
{
path: '/game',
element: <>game</>,
},
],
},
]);
Alternative: Using the New JSX Transform
If you are using React 17 or later and prefer not to import React explicitly in every file, you can configure your project to use the new JSX transform.
-
Ensure React Version:
- Make sure you're using React 17 or newer.
-
Update Babel Configuration:
- If you're using Babel, update your Babel configuration to use the new JSX runtime.
Example .babelrc:
{
"presets": [
[
"@babel/preset-react",
{
"runtime": "automatic"
}
]
]
}
-
Update TypeScript Configuration (if applicable):
- If you're using TypeScript, set the
jsx option to react-jsx or react-jsxdev in your tsconfig.json.
Example tsconfig.json:
{
"compilerOptions": {
"jsx": "react-jsx"
// other options...
}
}
-
Restart Your Development Server:
- After making these changes, restart your development server to apply the new configurations.
Recommendation
For immediate resolution and to ensure compatibility, import React explicitly in your files that use JSX. This approach works regardless of your React version and project configuration. However, if you're using React 17+ and wish to leverage the new JSX transform, ensure your project is properly configured as outlined above.
Final Adjusted Code with React Import:
import React from 'react';
import { createBrowserRouter } from 'react-router-dom';
import MainLayout from '@/shared/ui/main-layout/index.jsx';
export const router = createBrowserRouter([
{
path: '/',
element: <MainLayout />,
children: [
{
index: true,
element: <>page</>,
},
{
path: '/game',
element: <>game</>,
},
],
},
]);
Adding the import React from 'react'; line should resolve the React is not defined error.