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.

40 lines
906 B

2 years ago
  1. // 导入 React 包
  2. import React from 'react'
  3. // 函数组件的创建和渲染
  4. // 创建
  5. function Hello() {
  6. // 定义事件回调函数
  7. const clickHandle = () => {
  8. alert('函数组件中的事件被触发了')
  9. }
  10. // 事件绑定
  11. return <div onClick={clickHandle}>Hello everbody</div>
  12. }
  13. // 类组件的创建和渲染
  14. // 创建
  15. class HelloComponent extends React.Component {
  16. // 事件回调函数(标准写法,可以避免this指向问题)
  17. // 这样写 回调函数中的this指向的是当前的组件实例对象
  18. clickHandle = () => {
  19. alert('类组件中的事件被触发了')
  20. }
  21. render() {
  22. // 事件绑定
  23. return <div onClick={this.clickHandle}>this is class Component</div>
  24. }
  25. }
  26. function App() {
  27. return (
  28. <div>
  29. <Hello />
  30. <Hello></Hello>
  31. <HelloComponent />
  32. <HelloComponent></HelloComponent>
  33. </div>
  34. )
  35. }
  36. export default App