gpt4 book ai didi

javascript - React + TypeScript : 'React' refers to a UMD global but the current file is a module. 考虑改为添加导入。 (理由)

转载 作者:搜寻专家 更新时间:2023-10-30 21:26:21 26 4
gpt4 key购买 nike

我们在当前项目中使用 React 和 TypeScript,我遇到了以下行为。

import React, { Component } from 'react';

我将上面的行替换为下面的行,因为它似乎只使用 Component 导入。

import { Component } from 'react';

然后我被抛到下面的错误

Error: 'React' refers to a UMD global but the current file is a module. Consider adding an import instead.

我试着在 Stack 和 GitHub 上查找它,发现很少的文章和讨论,但仍然不清楚它为什么会发生。

是否会对 React 或 Typescript 抛出它以及为什么抛出它更感兴趣?不是修复它的方法。

还有一些关于 UMD 的光,为什么在这里使用它?

最佳答案

您是否在文件中渲染任何 jsx?如果是这样,您确实需要导入 React,因为这些 jsx 标记会编译成对 React.createElement() 的调用。如果没有导入,对 React 的引用会尝试引用一个全局变量,这会导致该错误。

如果您使用的是 eslint,我建议您使用 eslint-plugin-react 并启用规则 react-in-jsx-scope ,这将捕获这种情况。

关于javascript - React + TypeScript : 'React' refers to a UMD global but the current file is a module. 考虑改为添加导入。 (理由),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54770627/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com