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>
}
 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM