十七、TS在react中的使用 - 图1

类组件

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