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

2 years ago
  1. import React from 'react'
  2. // APP 为父组件,Son 为子组件
  3. // 父传子:props + 函数
  4. // 子传父:子组件通过父组件传递过来的函数,并且把想要传递的数据当成函数的实参传入即可
  5. function Son(props) {
  6. const { getSonMsg } = props
  7. return (
  8. <div>
  9. this is son
  10. <button onClick={() => getSonMsg('这里是来自于子组件中的数据')}>
  11. click
  12. </button>
  13. </div>
  14. )
  15. }
  16. class App extends React.Component {
  17. state = {
  18. list: [1, 2, 3],
  19. }
  20. // 1.准备一个函数,传给子组件
  21. getSonMsg = (SonMsg) => {
  22. console.log(SonMsg)
  23. }
  24. render() {
  25. return (
  26. <div>
  27. <Son getSonMsg={this.getSonMsg}></Son>
  28. </div>
  29. )
  30. }
  31. }
  32. export default App