You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
516 B

2 years ago
  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. // 组件的属性
  4. type Props = { name: string; age?: number }
  5. class Hello extends React.Component<Props> {
  6. // 在类组件中给 age 添加默认属性
  7. static defaultProps: Partial<Props> = {
  8. age: 20
  9. }
  10. render() {
  11. const { name, age } = this.props
  12. return (
  13. <div>{name}, { age } </div>
  14. )
  15. }
  16. }
  17. const App = () => <div>
  18. <Hello name="Ken"/>
  19. </div>
  20. ReactDOM.render(<App />, document.getElementById('root'))