【问题标题】:Type 'xxx' is missing the following properties from type 'ElementClass': context, setState, forceUpdate, props, and 2 more. TS2605“xxx”类型缺少“ElementClass”类型的以下属性:context、setState、forceUpdate、props 和另外 2 个。 TS2605
【发布时间】:2020-01-30 11:21:15
【问题描述】:

我在尝试运行我的 react 应用程序时收到以下错误:

D:/React/my-components/src/App.tsx D:/React/my-components/src/App.tsx(23,4) 中的 TypeScript 错误: JSX 元素类型“确认”不是 JSX 元素的构造函数。 “确认”类型缺少“ElementClass”类型的以下属性:上下文、setState、forceUpdate、props 和另外 2 个。 TS2605

21 |         </a>
22 |       </header>
23 |      <Confirm />
   |      ^
24 |     </div>
25 |   );
26 | }

这是我的确认组件代码

import * as React from "react";

class Confirm extends React.Component{
	 render(){
					return (
						<div className="confirm-wrapper confirm-visible">
			<div className="confirm-container">
			<div className="confirm-title-container">
			<span>This is where our title should go</span>
			</div>
			<div className="confirm-content-container">
			<p>This is where our content should go</p>
			</div>
			<div className="confirm-buttons-container">
			<button className="confirm-cancel">Cancel</button>
			<button className="confirm-ok">Okay</button>
			</div>
			</div>
			</div>
					);
	}
	
}
export default Confirm;

还有 App.tsx 代码

import React from 'react';
import logo from './logo.svg';
import './App.css';
import Confirm from './Confirm';

const App: React.FC = () => {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.tsx</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
	  <Confirm />
    </div>
  );
}

export default App;

这是我的 package.json

{
  "name": "my-components",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@types/jest": "24.0.18",
    "@types/node": "12.7.8",
    "@types/react": "16.9.4",
    "@types/react-dom": "16.9.1",
    "react-scripts": "3.1.2",
    "typescript": "3.6.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "react": "^16.10.1",
    "react-dom": "^16.10.1",
    "tslint": "^5.20.0",
    "tslint-config-prettier": "^1.18.0",
    "tslint-react": "^4.1.0"
  }
}

【问题讨论】:

    标签: reactjs typescript


    【解决方案1】:

    谢谢大家,我已经通过运行以下命令修复了这个错误

    纱线升级@types/react@latest

    【讨论】:

    • 遇到了同样的问题。这个修复也对我有用。但为什么?那么运行此命令时究竟发生了什么,为什么会弹出错误?有人可以在这里阐明一下吗?
    【解决方案2】:

    我有一个名为Component.tsx(驼峰式)的组件和一个名为component.ts(小写)的助手,这似乎使它们混淆了。我也在同一个文件中导入它们,所以我只是重命名了组件之一。

    【讨论】:

      猜你喜欢
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-11
      相关资源
      最近更新 更多