react中使用typescript时,error: Property 'setState' does not exist on type 'Home'

问题描述:

我在react中用typescript时,定义一个Home组件,然后在组件里用setState时会有这样一个报错:(如图)Property 'setState' does not exist on type 'Home'

分析解决:

  报错说我的Home组件上不存在setState属性,但是我把文件的后缀名从‘.tsx’改成‘.jsx’就不报这个错了,推断是typescript的类型检查报的这个错,识别不了react组件就不认为这个class函数上有setState属性。安装一下react、react-dom的类型定义就解决啦

npm install --save-dev  "@types/react"  "@types/react-dom"

猜你喜欢

转载自www.cnblogs.com/chen-cong/p/11997051.html
今日推荐