[英]React Converting JSX application to Typescript
I am keeping my code short as possible to remove confusion, actually i am trying to convert my application built on React jsx to React Typescript thus (tsx file).我尽可能保持代码简短以消除混淆,实际上我正在尝试将基于 React jsx 的应用程序转换为 React Typescript(tsx 文件)。
Error that i am receiving is - '[ts] Property ' state ' does not exist on type 'App'.any ALSO same for '[ts] Property ' setState ' does not exist on type 'App'.any' Please help me on this ...我收到的错误是 - '[ts] 类型'App'上不存在'[ts] 属性'状态'。'[ts] 类型'App'.any' 上不存在任何相同的属性' setState ' 请帮助我在这...
interface STATE {
loading: boolean
};
interface PROPS {};
export default class App extends Component<STATE, PROPS> {
constructor(props:any) {
super(props);
this.state = {
fruitsData : [],
loading: false
};
}
componentDidMount() {
this.setState({
loading: true
});
//Further functions present here plus call to service and binding the
data received to the array fruitsData
}
My package.json我的 package.json
{
"name": "example",
"version": "0.1.0",
"private": true,
"devDependencies": {
"@types/classnames": "^2.2.3",
"@types/node": "^4.0.35",
"classnames": "^2.2.5",
"gh-pages": "^0.12.0",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-scripts": "0.9.5",
"typescript": "^2.7.0-insiders.20171214"
},
"dependencies": {
"@types/react": "^16.0.34",
"@types/react-dom": "^16.0.3",
"awesome-typescript-loader": "^3.4.1",
"react-search-box": "0.0.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
}
}
First of all you must install @types/{react packages}
.首先,您必须安装
@types/{react packages}
。 Then you have to import React like that: import * as React from "react"
然后你必须像这样导入 React:
import * as React from "react"
Then your code would look like this:然后你的代码看起来像这样:
interface IAppState {
fruitsData: any[]; // change it for your type
loading: boolean;
}
interface IAppProps {
// your props
}
class App extends React.Component<IAppProps, IAppState> {
constructor(props: IAppProps) {
super(props);
this.state = {
fruitsData: [],
loading: false,
};
}
public componentDidMount() {
this.setState({
loading: true;
});
}
}
or you can write it without constructor like this:或者你可以在没有构造函数的情况下编写它,如下所示:
class App extends React.Component<IAppProps, IAppState> {
public state: IAppState = {
fruitsData: [],
loading: false,
}
public componentDidMount() {
this.setState({
loading: true;
});
}
}
Hope this will help you.希望这会帮助你。
First of all you should ensure that the type definitions for react
and react-dom
are installed.首先,您应该确保安装了
react
和react-dom
的类型定义。 With NPM you could do:使用 NPM,您可以:
npm install --save @types/{react,react-dom}
Then there are a few problems with your code, which I have corrected inline:然后您的代码存在一些问题,我已内联更正了这些问题:
interface State {
loading: boolean
// add fruitsData to state
fruitsData: string[] // or whatever type it is
}
interface Props {}
// Generic arguments are <Props, State>, not the other way around
export default class App extends Component<Props, State> {
constructor(props: Props) { // use the correct type here
super(props);
this.state = {
fruitsData : [],
loading: false
};
}
You were placing generic type PROPS & STATES at wrong position.您将泛型类型 PROPS & STATES 放置在错误的位置。
The first one is Props and second one is State.第一个是道具,第二个是状态。
export default class App extends Component<PROPS, STATE>
1). 1)。 run
跑
npm install --save typescript @types/node @types/react @types/react-dom @types/jest
or或
yarn add typescript @types/node @types/react @types/react-dom @types/jest
2). 2)。 Rename any file to be a TypeScript file (eg src/index.js to src/index.tsx)
将任何文件重命名为 TypeScript 文件(例如 src/index.js 到 src/index.tsx)
3). 3)。 Restart your development server!
重启你的开发服务器!
enjoy :)享受:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.