Keywords: React | JSX | Scope Error | ESLint Configuration | React Import
Abstract: This article provides an in-depth analysis of the common 'React must be in scope when using JSX' error in React development. Starting from JSX compilation principles, it explains the root causes of the error and offers multiple solutions. For different React versions and development environments, it introduces various repair methods including import statement correction, ESLint configuration updates, and dependency management to help developers completely resolve this common issue.
JSX Compilation Principles and Error Root Causes
In React development, JSX (JavaScript XML) serves as a syntax extension that allows developers to write HTML-like structures within JavaScript code. However, browsers do not directly understand JSX syntax, requiring compilation tools to transform it into standard JavaScript code.
Prior to React version 17.0, JSX compilation relied on the React.createElement() function. When the compiler encountered JSX code, it would transform it into corresponding React.createElement calls. For example:
// JSX code
<span>hello Tech View</span>
// Compiled JavaScript code
React.createElement('span', null, 'hello Tech View')
From this transformation process, it becomes evident that if the React variable is not defined in the current scope, the React.createElement call cannot execute, resulting in the 'React must be in scope when using JSX' error.
Basic Solution: Correct React Import
For developers using React versions before 17.0, the most direct solution is to ensure proper import of the React library. Common mistakes include import statement spelling errors or incorrect capitalization:
// Incorrect example ❌
import react, { Component } from 'react';
// Correct example ✅
import React, { Component } from 'react';
Note that React must be capitalized, as this follows JavaScript module import conventions. The corrected complete component example is as follows:
import React, { Component } from 'react';
import { render } from 'react-dom';
class TechView extends Component {
constructor(props) {
super(props);
this.state = {
name: 'Gopinath'
};
}
render() {
return (
<span>hello Tech View</span>
);
}
}
export default TechView;
Modern Solutions for React 17+
With the release of React 17.0, a new JSX transform mechanism was introduced. This new mechanism automatically imports special functions from new entry points in the React package, eliminating the need to explicitly import React in every file that uses JSX.
The working mechanism of the new transform is as follows:
// Original JSX code
function App() {
return <h1>Welcome!</h1>;
}
// New transform compilation result
import { jsx as _jsx } from 'react/jsx-runtime';
function App() {
return _jsx('h1', { children: 'Welcome!' });
}
Although the new mechanism removes the requirement for explicit React imports, code checking tools like ESLint may still report errors, requiring updates to relevant configurations.
ESLint Configuration Updates
For projects using React 17.0 and above, if scope errors persist, it's typically due to outdated ESLint configurations. Relevant rules can be disabled by modifying configuration files:
// .eslintrc.js or .eslintrc.json
{
"rules": {
"react/react-in-jsx-scope": "off",
"react/jsx-uses-react": "off"
}
}
Or configure in package.json:
{
"eslintConfig": {
"extends": ["react-app", "react-app/jest"],
"rules": {
"react/jsx-uses-react": "off",
"react/react-in-jsx-scope": "off"
}
}
}
Advanced Troubleshooting
If the above methods fail to resolve the issue, consider the following advanced troubleshooting steps:
Update React Scripts Version:
npm install react-scripts@latest
# or
yarn add react-scripts@latest
Clean and Reinstall Dependencies:
rm -rf node_modules package-lock.json
npm install
Update React Core Libraries:
npm install react@latest react-dom@latest
# For TypeScript projects
npm install --save-dev @types/react@latest @types/react-dom@latest
Best Practices and Prevention Measures
To avoid similar issues, developers are recommended to follow these best practices:
Regularly update project dependencies to the latest stable versions, particularly React and related toolchains. Establish unified code standards in team development to ensure all members follow the same import conventions. For new projects, prioritize React 17.0 and above to leverage the new JSX transform mechanism. Configure appropriate ESLint rule sets that ensure code quality without generating unnecessary error warnings.
By understanding JSX compilation principles and React version differences, developers can more confidently address the 'React must be in scope when using JSX' error and implement appropriate solutions.