Skip to content

ReferenceError: 组件是 不定义

错误信息

                组件是 不定义
              

快速摘要

你 referenced 组件 in JSX 但它 是从不 imported or declared, 所以 JavaScript doesn't know 什么它 是.

为什么发生

  • Forgot 到导入组件 at 顶部的 file

  • Typo in 组件 name (情况-sensitive — MyComponent 对比 myComponent)

  • 组件是 exported 从不同模块但 不 imported 这里

  • 默认对比 named 导出 mismatch

最小示例

✗ 错误代码
// Missing import
function App() {
  return <Header />; // Header is not defined
}
✓ 修复代码
import Header from "./Header";

function App() {
  return <Header />;
}

请求头 是 used 但从不 imported. 添加导入语句 at 顶部 fixes ReferenceError.

如何诊断

  • 检查导入语句 at 顶部的 file

  • 验证组件 name matches 导入精确地 (情况-sensitive)

  • 检查 whether file uses 导出默认 or named 导出

  • Look at 浏览器 console for 精确 undefined name

如何修复

  • 添加导入语句: 导入 MyComponent 从 './MyComponent'

  • Fix typo 到 match 精确组件 name

  • Use 正确导入语法 for 默认对比 named 导出

如何预防

  • Use IDE 使用 auto-导入 features

  • 启用 ESLint 使用 react/jsx-否-undef 规则

  • Use consistent naming 约定 (PascalCase for 组件)

相关资源

相关术语

相关课程

  • 组件

    学习如何 创建 and 导入组件.

← 返回语言