十七、TS在react中的使用

# 十七、TS在react中的使用

# 类组件
import React,{ Component } from 'react';
import { Button } from 'antd'
interface Greeting {
name: string;
firstName: string;
lastName: string;
}
interface State {
count: number
}
class HelloClass extends Component<Greeting, State> {
state: State = {
count: 0
}
static defaultProps = {
firstName: '',
lastName: ''
}
render() {
return (
<>
<p>你点击了 {this.state.count}</p>
<Button onClick={()=>{this.setState({count: this.state.count + 1})}}>Hello {this.props.name}</Button>
</>
)
}
}
export default HelloClass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
上次更新: 2021/04/22, 00:08:06