正文从这开始~

.tsx扩展名

为了在React TypeScript中解决Cannot find name报错,我们需要在使用JSX文件时使用.tsx扩展名,在你的tsconfig.json文件中把jsx设置为react-jsx ,并确保为你的应用程序安装所有必要的@types包。

下面是在名为App.ts的文件中发生错误的示例。

export default function App() {
// ️ Cannot find name 'div'.ts(2304)
return (
<div>
<input type="text" id="message" value="Initial value" />
{/* Cannot find name 'button'.ts(2304) */}
<button>Click</button>
</div>
);
}

上述示例代码的问题在于,我们的文件扩展名为.ts,但是我们在里面却写的JSX代码。

这是不被允许的,因此为了在TS文件中使用JSX,我们必须:

  1. 将文件命名为.tsx扩展名;
  2. tsconfig.json中启用jsx选项。

确保编写JSX代码的所有文件拥有.tsx扩展名。

// App.tsx

export default function App() {
return (
<div>
<input type="text" id="message" value="Initial value" />
<button>Click</button>
</div>
);
}

如果在更新文件扩展名为.tsx后,问题依然没有解决,请尝试重启IDE和开发服务器。

tsconfig.json配置文件

打开tsconfig.json文件,确保jsx选项设置为react-jsx

{
"compilerOptions": {
"jsx": "react-jsx", // ️ make sure you have this
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
},
"include": ["src/**/*"]
}

jsx选项设置为react-jsx ,它会导致编译器使用JSX,将.js文件改为_jsx调用

安装@types依赖包

另一个导致Cannot find name错误的原因是,我们没有安装必要的@types/

在项目的根目录下打开终端,运行下面的命令:

# ️ with NPM
npm install --save-dev @types/react @types/react-dom @types/node @types/jest typescript # ------------------------------------------------------ # ️ with YARN
yarn add @types/react @types/react-dom @types/node @types/jest typescript --dev

该命令安装了reactreact-domnodejest 的类型声明文件,同时也安装了typescript

如果依旧报错,请尝试删除node_modulespackage-lock.json(不是package.json)文件,重新运行npm install 并重启IDE。

# ️ delete node_modules and package-lock.json
rm -rf node_modules
rm -f package-lock.json # ️ clean npm cache
npm cache clean --force npm install

如果错误依旧存在,请确保重启IDE和开发服务器。VSCode经常出现故障,有时重新启动就能解决问题。

如果问题依旧存在,打开package.json 文件,确保下面的依赖包被包含在devDependencies对象中。

{
// ... rest
"devDependencies": {
"@types/react": "^17.0.44",
"@types/react-dom": "^17.0.15",
"@types/jest": "^27.4.1",
"@types/node": "^17.0.23",
"typescript": "^4.6.3"
}
}

可以手动添加上述依赖,并重新运行npm install

npm install

或者安装下面依赖的最新版:

# ️ with NPM
npm install --save-dev @types/react@latest @types/react-dom@latest @types/node@latest @types/jest@latest typescript@latest # ------------------------------------------------------ # ️ with YARN
yarn add @types/react@latest @types/react-dom@latest @types/node@latest @types/jest@latest typescript@latest --dev

React报错之Cannot find name的更多相关文章

  1. react 报错的堆栈处理

    react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...

  2. React报错 :browserHistory doesn't exist in react-router

    由于版本问题,React中history不可用 import { hashHistory } from 'react-router' 首先应该导入react-router-dom包: import { ...

  3. react报错 TypeError: Cannot read property 'setState' of undefined

    代码如下: class test extends Component { constructor(props) { super(props); this.state = { liked: false ...

  4. React报错之Cannot find namespace context

    正文从这开始~ 总览 在React中,为了解决"Cannot find namespace context"错误,在你使用JSX的文件中使用.tsx扩展名,在你的tsconfig. ...

  5. React报错之Expected `onClick` listener to be a function

    正文从这开始~ 总览 当我们为元素的onClick属性传递一个值,但是该值却不是函数时,会产生"Expected onClick listener to be a function" ...

  6. React报错:Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix,

    今天在开发时报了以下错误,记录一下 我们不能在组件销毁后设置state,防止出现内存泄漏的情况 出现原因直接告诉你了,组件都被销毁了,还设置个锤子的state啊 解决方案: 利用生命周期钩子函数:co ...

  7. react报错this.setState is not a function

    当报错这个的时候就要看函数是否在行内绑定this,或者在constructor中绑定this. 我这里犯的错误的是虽然我在constructor中绑定了this,但是语法写的不正确. 错误示范: co ...

  8. React报错:Laravel React-Router browserHistory 刷新子页面报错404

    举例:myblog.com/ 刷新没问题 myblog.com/add 刷新404 browserHistory报404,hashHistory却正常 原因是少路由.web.php添加路由 Route ...

  9. React报错之组件不能作为JSX组件使用

    正文从这开始~ 总览 组件不能作为JSX组件使用,出现该错误有多个原因: 返回JSX元素数组,而不是单个元素. 从组件中返回JSX元素或者null以外的任何值. 使用过时的React类型声明. 返回单 ...

随机推荐

  1. spring boot 统一接口异常返回值

    创建业务 Exception 一般在实际项目中,推荐创建自己的 Exception 类型,这样在后期会更容易处理,也比较方便统一,否则,可能每个人都抛出自己喜欢的异常类型,而造成代码混乱 Servic ...

  2. 解决 js aysnc await try-catch 地狱

  3. 136. Single Number - LeetCode

    Question 136. Single Number Solution 思路:构造一个map,遍历数组记录每个数出现的次数,再遍历map,取出出现次数为1的num public int single ...

  4. CMake进行C/C++开发(linux下)

    开发环境配置 安装GCC,GDB sudo apt update # 通过以下命令安装编译器和调试器 sudo apt install build-essential gdb 安装成功确认 # 以下命 ...

  5. 浏览器上写代码,4核8G微软服务器免费用,Codespaces真香

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 一图胜千言 先上图,下面是欣宸在自己的iPad Pro ...

  6. Python Django 功能模块

    Python Django模块 Django模块,是针对有django基础,对django功能进行模块化,方便下次使用. 一.注册模块 该注册采用邮箱验证,注册成功后会发送激活链接到邮箱. 邮箱验证参 ...

  7. springcloud-- Alibaba-nacos--支持的几种服务消费方式

    通过<Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现>一文的学习,我们已经学会如何使用Nacos来实现服务的注册与发现,同时也介绍如何通过LoadBal ...

  8. 论文解读(SUBLIME)《Towards Unsupervised Deep Graph Structure Learning》

    论文信息 论文标题:Towards Unsupervised Deep Graph Structure Learning论文作者:Yixin Liu, Yu Zheng, Daokun Zhang, ...

  9. 在公网服务器搭建CobaltStrike4.0

    因为工作需要使用cs,正好之前腾讯云薅了一把羊毛,就把VPS装起来cs. 选的环境是centos7.6 cs运行需要java环境 先使用yum -y list java* 查看yum存在的java库 ...

  10. MySQL根据表前缀批量修改、删除表

    注意:请先调试好,以及做好备份,再执行操作. 批量修改表 批量给前缀为 xushanxiang_content_ 的表增加一个 username 的字段: SELECT CONCAT('ALTER T ...