• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
    當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

    react 組件傳值的三種方法

    來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 21:55:47
    文檔

    react 組件傳值的三種方法

    react 組件傳值的三種方法:整理 react 組件傳值 三種方式 父組件向子組件傳值(通過(guò)props傳值) 子組件: class Children extends Component{ constructor(props){ super(props); } render(){ return( <div>這是:{this.props.name}
    推薦度:
    導(dǎo)讀react 組件傳值的三種方法:整理 react 組件傳值 三種方式 父組件向子組件傳值(通過(guò)props傳值) 子組件: class Children extends Component{ constructor(props){ super(props); } render(){ return( <div>這是:{this.props.name}

    整理 react 組件傳值 三種方式

    父組件向子組件傳值(通過(guò)props傳值)

    子組件:

     class Children extends Component{
     constructor(props){
     super(props);
     }
     render(){
     return(
     <div>這是:{this.props.name}</div> // 這是 父向子
     )
     }
     }
    

    父組件:

     class App extends React.Component{
     render(){
     return(
     <div>
     <Children name="父向子"/>
     </div>
     )
     }
     }
    

    父組件向子組件傳值(回調(diào)函數(shù))

    子組件

     class Children extends Component{
     constructor(props){
     super(props);
     }
     handerClick(){
     this.props.changeColor('skyblue') // 執(zhí)行父組件的changeColor 并傳參 必須和父組件中的函數(shù)一模一樣
     }
     render(){
     return(
     <div>
     <div>父組件的背景色{this.props.bgcolor}</div> // 子組件接收父組件傳過(guò)來(lái)的值 bgcolor
     <button onClick={(e)=>{this.handerClick(e)}}>改變父組件背景</button> // 子組件執(zhí)行函數(shù)
     </div>
     )
     }
     }
    

    父組件

     class Father extends Component{
     constructor(props){
     super(props)
     this.state = {
     bgcolor:'pink'
     }
     }
     bgChange(color){
     this.setState({
     bgcolor:color
     })
     }
     render(props){
     <div style={{background:this.state.bgcolor}}>
     // 給子組件傳遞的值 color 
     <Children bgcolor={this.state.bgcolor} changeColor={(color)=>{this.bgChange(color)}} /> 
     // changeColor 子組件的參數(shù)=color 當(dāng)做形參
     </div>
     }
     }
    

    兄弟組件傳值(子傳給父,父再傳給另一個(gè)子)

    子組件1

     class Children1 extends Component{
     constructor(props){
     super(props);
     }
     handerClick(){
     this.props.changeChild2Color('skyblue') 
     // 改變兄弟組件的顏色 把changeChild2Color的參數(shù)傳給父
     }
     render(){
     return(
     <div>
     <div>children1</div>
     <button onClick={(e)=>{this.handerClick(e)}}>改變children2背景</button>
     </div>
     )
     }
     }
    

    子組件2

     class Children2 extends Component{
     constructor(props){
     super(props);
     }
     render(){
     return(
     <div style={{background:this.props.bgcolor}}>
     // 從父元素獲取自己的背景色
     <div>children2 背景色 {this.props.bgcolor}</div>
     // children2 背景色 skyblue
     </div>
     )
     }
     } 
    

    父組件

    class Father extends Component{
     constructor(props){
     super(props)
     this.state = {
     child2bgcolor:'pink'
     }
     }
     onchild2bgChange(color){
     this.setState({
     child2bgcolor:color
     })
     }
     render(props){
     <div>
     <Children1 changeChild2Color={(color)=>{this.onchild2bgChange(color)}} />
     <Children2 bgcolor={this.state.child2bgcolor} />
     </div>
     }
    }
    

    聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    react 組件傳值的三種方法

    react 組件傳值的三種方法:整理 react 組件傳值 三種方式 父組件向子組件傳值(通過(guò)props傳值) 子組件: class Children extends Component{ constructor(props){ super(props); } render(){ return( <div>這是:{this.props.name}
    推薦度:
    標(biāo)簽: 方法 方式 三種方式
    • 熱門(mén)焦點(diǎn)

    最新推薦

    猜你喜歡

    熱門(mén)推薦

    專題
    Top
    主站蜘蛛池模板: 午夜精品久久久久久影视777 | 精品无码久久久久国产| 91麻豆精品一二三区在线| 亚洲麻豆精品国偷自产在线91| 97精品在线播放| 国产美女精品一区二区三区 | 国产午夜精品理论片 | 91久久婷婷国产综合精品青草| 亚洲av无码成人精品区| 国产精品日韩深夜福利久久 | 亚洲欧美一级久久精品| 精品成人一区二区三区四区| 2021国产成人精品久久| 99精品免费视频| 精品久久久久久无码中文字幕一区| 亚洲精品国产福利一二区| 久久亚洲av无码精品浪潮| 国产亚州精品女人久久久久久| 精品精品国产国产| 97视频在线观看这里只有精品 | 亚洲国产精品一区| 国产精品国产三级国产专播 | 久久精品国产只有精品66| 99精品国产福利在线观看| 精品无人区麻豆乱码1区2区| 2021国产成人精品国产| 国语自产少妇精品视频蜜桃| 久久婷婷国产综合精品| 亚洲国产精品久久电影欧美| 亚洲午夜精品久久久久久浪潮| 四虎国产精品永久地址入口| 免费人成在线观看欧美精品| 精品人体无码一区二区三区| 精品伦精品一区二区三区视频| 精品无码国产自产拍在线观看蜜 | 无码欧精品亚洲日韩一区| 亚洲精品无码永久在线观看 | 久久久久久九九99精品| 精品亚洲A∨无码一区二区三区| 久久婷婷国产综合精品| 国产精品视频第一区二区三区|