ReferenceError: React 是不 定义
错误信息
React 是不 定义
快速摘要
JSX 需要 React 到是 in 作用域 (in older setups) or automatic JSX 运行时到 是配置. 这个错误平均值 neither 是 satisfied.
为什么发生
Forgot 到导入 React in file using JSX (older transform)
构建 tool 不配置 for automatic JSX 运行时
React 导入是 accidentally 移除
最小示例
✗ 错误代码
// No import
function App() {
return <div>Hello</div>; // React is not defined
}✓ 修复代码
import React from "react";
function App() {
return <div>Hello</div>;
}使用 classic JSX transform, React 必须是 in 作用域. 添加导入 fixes 它. 使用 automatic 运行时 (React 17+), 导入是 添加 automatically.
如何诊断
检查如果
导入 React 从 'react'是 present at 顶部的 file验证 JSX transform 设置 in 你的构建配置 (babel/tsconfig)
检查如果 file uses JSX 但有 否 React 导入
如何修复
添加
导入 React 从 'react'at 顶部的 file配置 automatic JSX 运行时 in 你的构建 tool
For TypeScript: 设置
jsx: 'react-jsx'in tsconfig.JSON
如何预防
Use automatic JSX 运行时 (React 17+) 到 avoid manual 导入
配置 ESLint 到 detect missing React 导入