• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題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關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    react 組件傳值的三種方法

    來源:懂視網 責編:小采 時間:2020-11-27 21:55:47
    文檔

    react 組件傳值的三種方法

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

    整理 react 組件傳值 三種方式

    父組件向子組件傳值(通過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>
     )
     }
     }
    

    父組件向子組件傳值(回調函數)

    子組件

     class Children extends Component{
     constructor(props){
     super(props);
     }
     handerClick(){
     this.props.changeColor('skyblue') // 執行父組件的changeColor 并傳參 必須和父組件中的函數一模一樣
     }
     render(){
     return(
     <div>
     <div>父組件的背景色{this.props.bgcolor}</div> // 子組件接收父組件傳過來的值 bgcolor
     <button onClick={(e)=>{this.handerClick(e)}}>改變父組件背景</button> // 子組件執行函數
     </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 子組件的參數=color 當做形參
     </div>
     }
     }
    

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

    子組件1

     class Children1 extends Component{
     constructor(props){
     super(props);
     }
     handerClick(){
     this.props.changeChild2Color('skyblue') 
     // 改變兄弟組件的顏色 把changeChild2Color的參數傳給父
     }
     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>
     }
    }
    

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    react 組件傳值的三種方法

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

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 日韩精品国产另类专区| 色妞ww精品视频7777| 久久国产午夜精品一区二区三区| 久久r热这里有精品视频| 日韩精品极品视频在线观看免费| 国内精品欧美久久精品| 国产人妖乱国产精品人妖| 日本精品一区二区三区在线视频一| 国产亚洲一区二区精品| 欧美精品v国产精品v日韩精品| 日韩美女18网站久久精品| 国产欧美一区二区精品性色99 | 国产成人精品2021| 亚洲精品乱码久久久久66| 蜜臀精品国产高清在线观看| 国产成人精品久久亚洲高清不卡 | 久久99国产精品成人欧美| 亚洲精品线在线观看| 国产第一福利精品导航| 精品熟女少妇a∨免费久久| 亚洲一区无码精品色| 国产91精品一区二区麻豆亚洲| 99久久精品午夜一区二区| 久久久久99精品成人片直播| 亚洲中文字幕无码久久精品1| 香港aa三级久久三级老师2021国产三级精品三级在 | 四虎国产精品永久在线观看| 亚洲精品成人网久久久久久| 色偷偷88欧美精品久久久 | 99re国产精品视频首页| 国产成人亚洲合集青青草原精品 | 亚洲第一区精品观看| 四虎精品亚洲一区二区三区| 欧美日韩精品系列一区二区三区国产一区二区精品| 国产三级精品三级| 蜜桃麻豆www久久国产精品 | 国产韩国精品一区二区三区| 亚洲愉拍自拍欧美精品| 91精品免费久久久久久久久| 国产精品国产三级国产| 国产网红主播无码精品|