react函数式组件传值

父组件

引入子组件,子组件的名字一定要大写 如Header

import Header from '../../compontens/header/Header' 
export default function App() {

  function getChildrenData(e) {
    console.log('我是子组件的值',e)
  }

  return <div className="container">
    <Header name="欢迎来到"  getChildrenData = { getChildrenData } />
  </div>
}
 
子组件
 
import React from 'react'

export default function Header(props) {

    const { getChildrenData } = props;

    const str = '我是子组件传给父组件的值'

    return <div>
        <h1 >{ props.name }</h1>
        <button onClick={ () => getChildrenData(str) }>子组件传递给父组件的值</button>
    </div>
}
 
 
 
 
 
 
posted @ 2020-10-22 17:23  起风了1573  阅读(7451)  评论(1)    收藏  举报