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
664 B

2 years ago
  1. import ReactDOM from "react-dom";
  2. // 函数的默认值
  3. type Props = { name: string; age?: number}
  4. // const Hello: FC<Props> = ( { name, age } ) => (
  5. // <div>我是:{name}, 我 { age } 岁</div>
  6. // )
  7. // 提供默认属性
  8. // Hello.defaultProps = {
  9. // age: 18 // age 是可选属性,当没给 age 的值时,默认 age 的值为 18
  10. // }
  11. // 简化写法
  12. const Hello = ( { name, age = 18 }: Props ) => ( // 给 age 添加默认值 18
  13. <div>{name}, { age } </div>
  14. )
  15. const App = () => <div>
  16. {/* name 是必填的 */}
  17. <Hello name="jack" />
  18. </div>
  19. ReactDOM.render(<App />, document.getElementById('root'))