【问题标题】:Error: 'React' must be in scope when using JSX错误:使用 JSX 时,\'React\' 必须在范围内
【发布时间】:2023-01-21 06:45:42
【问题描述】:

在 Windows 8.1 上使用 VScode 在 debian 10 上使用 React 制作测试项目,当在项目文件夹中使用命令 yarn start 时,出现错误:

./src/App.js
  Line 6:    'React' must be in scope when using JSX  react/react-in-jsx-scope
  Line 7:    'React' must be in scope when using JSX  react/react-in-jsx-scope
  Line 8:    'React' must be in scope when using JSX  react/react-in-jsx-scope
  Line 9:    'React' must be in scope when using JSX  react/react-in-jsx-scope
  Line 10:  'React' must be in scope when using JSX  react/react-in-jsx-scope
  Line 12:   'React' must be in scope when using JSX  react/react-in-jsx-scope

import React from 'react';"react/react-in-jsx-scope": "off" 的解决方案无效。还有什么问题?

【问题讨论】:

  • import React from 'react'; 绝对应该摆脱这个错误。 (我假设发生这种情况是因为您使用的是 React 16 或更早版本。)请向我们展示您的 App.js,包括此导入,以及运行它时遇到的错误(假设它与上面的不同,它当然应该是)。
  • 也许我遗漏了一些东西,或者你可能正在使用 WSL。但我不明白你是如何在 Windows 8.1 上的 Debian 中创建 React 程序的(它必须是 WSL,对吗?)如果是这样你应该说明,如果不是,请澄清。
  • @J-D3V 我不确定是不是因为你使用的是 WSL。我正在使用 Windows,但仍然遇到相同的错误 (React 18.0.2)。我不知道为什么。

标签: javascript reactjs


【解决方案1】:
  1. 在 js 文件的开头部分添加。

    从“反应”导入反应

    这将解决问题

【讨论】:

  • 您的答案可以通过其他支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写出好的答案的信息in the help center
  • 问题解决了我刚刚更新了 yarn 和 nodeJS
【解决方案2】:

我遇到了同样的问题,通过运行 npm audit fix --force 解决了。 我会尝试重新安装软件包。

【讨论】:

    猜你喜欢
    • 2017-07-27
    • 2022-11-17
    • 2020-09-11
    • 2020-01-11
    • 2019-10-26
    • 2018-08-22
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多