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.

25 lines
576 B

2 years ago
  1. import ReactDOM from "react-dom";
  2. import { FC } from "react";
  3. // React 函数组件的类型以及组件的属性
  4. type Props = { name: string; age?: number}
  5. // 第一种:
  6. const Hello: FC<Props> = ( { name, age } ) => (
  7. <div>{name}, { age } </div>
  8. )
  9. // 第二种(简化):
  10. const HI = ({ name, age }: Props) => (
  11. <div>
  12. {name}, { age }
  13. </div>
  14. )
  15. const App = () => <div>
  16. {/* name 是必填的 */}
  17. <Hello name="jack" age={ 30 }/>
  18. <HI name="Ken" age={ 20 }/>
  19. </div>
  20. ReactDOM.render(<App />, document.getElementById('root'))