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.

36 lines
929 B

2 years ago
  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. // 事件绑定和事件对象
  4. type Props = { name: string; age?: number}
  5. const Hello = ( { name, age = 18 }: Props ) => {
  6. // const onClick = () => {
  7. // console.log('赞!')
  8. // }
  9. const onClick = (e: React.MouseEvent<HTMLButtonElement>) => {
  10. console.log('赞!', e.currentTarget)
  11. }
  12. const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  13. console.log(e.target.value)
  14. }
  15. return (
  16. <div>
  17. {name}, { age }
  18. <button onClick={onClick}></button>
  19. <input onChange={onChange} />
  20. {/* 可以利用 TS 的类型推论来查看事件对象类型(将鼠标放在 e 上可以查看) */}
  21. {/* <input onChange={ e => {} } /> */}
  22. </div>
  23. )
  24. }
  25. const App = () => <div>
  26. {/* name 是必填的 */}
  27. <Hello name="jack" />
  28. </div>
  29. ReactDOM.render(<App />, document.getElementById('root'))