日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ReactJS組件之間如何進(jìn)行通信

最近在學(xué)習(xí)react.js,不得不說***次接觸組件化開發(fā)很神奇,當(dāng)然也很不習(xí)慣。

成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計制作、成都做網(wǎng)站與策劃設(shè)計,連云港網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:連云港等地區(qū)。連云港做網(wǎng)站價格咨詢:13518219792

react的思想還是蠻獨特的,當(dāng)然圍繞react的一系列自動化工具也是讓我感覺亞歷山大

今天總結(jié)一下react組件之間的通信,權(quán)當(dāng)是自己的學(xué)習(xí)筆記:

reactJs中數(shù)據(jù)流向的的特點是:單項數(shù)據(jù)流

react組件之間的組合不知道為什么給我一種數(shù)據(jù)結(jié)構(gòu)當(dāng)中樹的感覺,數(shù)據(jù)就是從根節(jié)點(頂端或其他子樹的頂端)“流”下來,大概就是這個樣子:

 比如這是一個組件樹,數(shù)據(jù)就可以從main組件流到j(luò)umbotron組件、queationList組件、form組件,類似的queation組件的數(shù)據(jù)也可以流到下邊的question組件里邊。

但是很遺憾,這個從上到下的數(shù)據(jù)流動,只能解決很少的問題,還有一部分的數(shù)據(jù)流動就是類似從jumbotron組件到form組件的這樣的兄弟組件的流動形式,或者隔著幾個層級的數(shù)據(jù)流動,再或者子組件發(fā)生了數(shù)據(jù)的變化,想通知父組件,數(shù)據(jù)流向從子組件到父組件流,這些問題才是大多數(shù)的開發(fā)者需要面臨的問題。所以這篇筆記總結(jié)下基礎(chǔ)的組件通信:

數(shù)據(jù)從父組件到子組件

 最簡單的通信就是父子間的通信,比如上邊圖上的有個jsonObj從main流進(jìn)了QueationList參考代碼:

 
 
  1. //這里模擬出幾條數(shù)據(jù) 
  2. var jsonObj=[ 
  3.     {name:"A",question:"從小被人打怎么辦?",TextArea:"習(xí)慣就好了",applaud:35,disagree:1}, 
  4.     {name:"B",question:"長的太帥被人砍怎么辦?",TextArea:"食屎啦你",applaud:35,disagree:10}, 
  5.     {name:"C",question:"因為太胖被人摸怎么辦?",TextArea:"享受就好了",applaud:35,disagree:45}, 
  6.     {name:"D",question:"被老師打不開心",TextArea:"用錢打臉",applaud:35,disagree:6}, 
  7.     {name:"E",question:"不愛洗澡怎么辦?",TextArea:"打一頓就好了",applaud:35,disagree:9} 
  8.  
  9. var QuestionList=React.createClass({ 
  10.     prepareToRender:function(list){ 
  11.         var array=[]; 
  12.         for(var i=0;i
  13.             array.push(); 
  14.         } 
  15.         return array; 
  16.     }, 
  17.     render:function(){ 
  18.         var array=this.prepareToRender(this.props.jsonObj); 
  19.         return 
    {array}
  20.     } 
  21. }); 
  22. var Main = React.createClass({ 
  23.     //開始渲染 
  24.     render: function () { 
  25.         return ( 
  26.             
     
  27.                  
  28.                      
  29.                          
  30.                     
 
  •             
  •  
  •         ); 
  •     } 
  • }); 
  • ReactDOM.render( 
  •     
  •     document.getElementById('container') 
  • );  
  • 代碼寫的不怎么規(guī)范,但是數(shù)據(jù)的傳遞就是這樣的:

    這樣就可以把父組件的數(shù)據(jù)帶到子組件里邊

    數(shù)據(jù)從子組件到父組件

    理論上來說數(shù)據(jù)只能是單向的,所以不借助插件數(shù)據(jù)還真不好從子組件到父組件,一種很簡單的手段是回調(diào)函數(shù):

    在父組件當(dāng)中寫個回調(diào)函數(shù),然后傳遞到子組件,什么時候子組件數(shù)據(jù)變化了,直接調(diào)這個回調(diào)函數(shù)就可以了。

     比如現(xiàn)在的jumbotron的按鈕被點擊了,我們想把被點擊這個事件發(fā)給它的父組件也就是main組件,那么我們可以這個做:

     
     
    1. var Jumbotron = React.createClass({ 
    2.     handleClick: function () { 
    3.         this.props.openTheWindow(false); 
    4.     }, 
    5.     render: function () { 
    6.         return ( 
    7.                  
    8.                      
    9.                         開始體驗 
    10.                          
    11.                     
     
  •                 
  •  
  •             
  •  
  •         ); 
  •     } 
  • }); 
  •  
  • var Main = React.createClass({ 
  •     getInitialState: function () { 
  •         return { 
  •             openTheWindow: true 
  •         }; 
  •     }, 
  •     //開始給子組件一個回調(diào)函數(shù),用來做子組件給父組件通信使用 
  •     buttonResponse:function(windowSatus){ 
  •         this.setState({openTheWindow : windowSatus}); 
  •     }, 
  •     //開始渲染 
  •     render: function () { 
  •         console.log(jsonObj) 
  •         return ( 
  •             
     
  •                  
  •             
  •  
  •         ); 
  •     } 
  • }); 
  • ReactDOM.render( 
  •     
  •     document.getElementById('container') 
  • );  
  • 子組件通知父組件狀態(tài)變化就是這樣,就好像是兒子找爸爸要零花錢,零花錢以及給不給都是爸爸說了算的。

    兄弟組件之間的通信

    這個其實應(yīng)該是一個動態(tài)應(yīng)用中最常見的通信,比如jubotron組件的點擊按鈕,form組件的表單出現(xiàn):

    這就是一個典型的兄弟之間的通信:

     兄弟節(jié)點其實可以就是子父通信&&父子通信的疊加

    首先按鈕被點擊,子組件通知負(fù)組件這個事件,然后父組件把這個消息帶給另一個子組件

    下邊是個點擊按鈕顯示表單的例子:

     
     
    1. /** 
    2.  * Created by niuGuangzhe on 2016/9/10. 
    3.  */ 
    4. var Jumbotron = React.createClass({ 
    5.     handleClick: function () { 
    6.         this.props.openTheWindow(false); 
    7.     }, 
    8.     render: function () { 
    9.         return ( 
    10.              
    11.                  
    12.                      
    13.                         開始體驗 
    14.                          
    15.                     
     
  •                 
  •  
  •             
  •  
  •         ); 
  •     } 
  • }); 
  •  
  • var Form = React.createClass({ 
  •     getInitialState:function(){ 
  •         return { 
  •             inputTitle:"請輸入標(biāo)題", 
  •             mainBody:"在此輸入正文" 
  •         }; 
  •     }, 
  •     //點擊按鈕觸發(fā)事件:清除所有已經(jīng)輸入的文字 
  •     cleanText:function(){ 
  •         this.setState({ 
  •             inputTitle:"", 
  •             mainBody:""}); 
  •     }, 
  •     //表單監(jiān)視事件 
  •     handleChange(name,e) { 
  •         var newState = {}; 
  •         console.log(name); 
  •         newState[name] =event.target.value; 
  •         this.setState(newState); 
  •     }, 
  •     render: function () { 
  •         return ( 
  •              
  •                  
  •                      
  •                         標(biāo)題 
  •                          
  •                     
  •  
  •  
  •                      
  •                         正文 
  •                          
  •                     
  •  
  •                      
  •  
  •                          
  •                          
  •                     
  •  
  •                  
  •              
  •         ) 
  •     } 
  • }) 
  •  
  •  
  • var Main = React.createClass({ 
  •     getInitialState: function () { 
  •         return { 
  •             openTheWindow: true 
  •         }; 
  •     }, 
  •     //開始給子組件一個回調(diào)函數(shù),用來做子組件給父組件通信使用 
  •     buttonResponse:function(windowSatus){ 
  •         this.setState({openTheWindow : windowSatus}); 
  •     }, 
  •     //開始渲染 
  •     render: function () { 
  •         console.log(jsonObj) 
  •         return ( 
  •             
     
  •                  
  •                  
  •                      
  •                  
  •              
  •         ); 
  •     } 
  • }); 
  • ReactDOM.render( 
  •     
  •     document.getElementById('container') 
  • );  
  • 就是這樣,

    其實上邊的代碼是我從之前的沒事干做的一個單頁面上拿過來改的,為了不出現(xiàn)代碼無法運行的問題,下邊貼出所有代碼:

     
     
    1. /** 
    2.  * Created by niuGuangzhe on 2016/9/10. 
    3.  */ 
    4. var Jumbotron = React.createClass({ 
    5.     handleClick: function () { 
    6.         this.props.openTheWindow(false); 
    7.     }, 
    8.     render: function () { 
    9.         return ( 
    10.              
    11.                  
    12.                      
    13.                         React+bootstrap簡單實例 
    14.                      
    15.                  
    16.                  
    17.                      
    18.                         上手體驗:***次嘗試組件化開發(fā)

       
    19.                      
    20.                  
    21.                  
    22.                      
    23.                         開始體驗 
    24.                          
    25.                      
    26.                  
    27.              
    28.         ); 
    29.     } 
    30. }); 
    31.  
    32. var Form = React.createClass({ 
    33.     getInitialState:function(){ 
    34.         return { 
    35.             inputTitle:"請輸入標(biāo)題", 
    36.             mainBody:"在此輸入正文" 
    37.         }; 
    38.     }, 
    39.     //點擊按鈕觸發(fā)事件:清除所有已經(jīng)輸入的文字 
    40.     cleanText:function(){ 
    41.         this.setState({ 
    42.             inputTitle:"", 
    43.             mainBody:""}); 
    44.     }, 
    45.     //表單監(jiān)視事件 
    46.     handleChange(name,e) { 
    47.         var newState = {}; 
    48.         console.log(name); 
    49.         newState[name] =event.target.value; 
    50.         this.setState(newState); 
    51.     }, 
    52.     render: function () { 
    53.         return ( 
    54.              
    55.                  
    56.                      
    57.                         標(biāo)題 
    58.                          
    59.                      
    60.  
    61.                      
    62.                         標(biāo)題 
    63.                          
    64.                      
    65.                      
    66.  
    67.                          
    68.                          
    69.                      
    70.                  
    71.              
    72.         ) 
    73.     }, 
    74.     //監(jiān)測從新渲染 
    75.     componentDidUpdate:function(){ 
    76.         console.log("子組件重新渲染;"); 
    77.     } 
    78. }) 
    79.  
    80. var Question = React.createClass({ 
    81.     getInitialState : function(){ 
    82.         return { 
    83.             click:true, 
    84.             disClick:true 
    85.         }; 
    86.     }, 
    87.     numberHandle:function(){ 
    88.         if(this.state.click===true){ 
    89.             //奇數(shù)次點擊,開始增加數(shù)據(jù) 
    90.             this.props.obj.applaud+=1; 
    91.             this.setState({click:false}); 
    92.         }else{ 
    93.             //偶數(shù)次點擊,減去數(shù)據(jù) 
    94.             this.props.obj.applaud-=1; 
    95.             this.setState({click:true}); 
    96.         } 
    97.     }, 
    98.     decreateHandle:function(){ 
    99.         if(this.state.disClick===true){ 
    100.             //奇數(shù)次點擊,開始增加數(shù)據(jù) 
    101.             this.props.obj.applaud-=1; 
    102.             this.setState({disClick:false}); 
    103.         }else{ 
    104.             //偶數(shù)次點擊,減去數(shù)據(jù) 
    105.             this.props.obj.applaud+=1; 
    106.             this.setState({disClick:true}); 
    107.         } 
    108.     }, 
    109.     render: function () { 
    110.         return ( 
    111.              
    112.                  
    113.                      
    114.                         {this.props.obj.applaud-this.props.obj.disagree}
    115.                             className="glyphicon glyphicon-chevron-up"> 
    116.                      
    117.                      
    118.                      
    119.                         
    120.                             className="glyphicon glyphicon-chevron-down"> 
    121.                          
    122.                      
    123.                  
    124.                  
    125.                     

      {this.props.obj.question}

       
    126.                     

      {this.props.obj.TextArea}

       
    127.                  
    128.              
    129.         ); 
    130.     } 
    131. }); 
    132.  
    133. var QuestionList=React.createClass({ 
    134.     prepareToRender:function(list){ 
    135.         var array=[]; 
    136.         for(var i=0;i
    137.             array.push(); 
    138.         } 
    139.         return array; 
    140.     }, 
    141.     render:function(){ 
    142.         var array=this.prepareToRender(this.props.jsonObj); 
    143.         return 
      {array}
    144.     } 
    145. }); 
    146.  
    147.  
    148. //這里模擬出幾條數(shù)據(jù) 
    149. var jsonObj=[ 
    150.     {name:"A",question:"從小被人打怎么辦?",TextArea:"習(xí)慣就好了",applaud:35,disagree:1}, 
    151.     {name:"B",question:"長的太帥被人砍怎么辦?",TextArea:"食屎啦你",applaud:35,disagree:10}, 
    152.     {name:"C",question:"因為太胖被人摸奶怎么辦?",TextArea:"享受就好了",applaud:35,disagree:45}, 
    153.     {name:"D",question:"被老師打不開心",TextArea:"用錢打ta臉",applaud:35,disagree:6}, 
    154.     {name:"E",question:"不愛洗澡怎么辦?",TextArea:"打一頓就好了",applaud:35,disagree:9} 
    155.  
    156. var Main = React.createClass({ 
    157.     getInitialState: function () { 
    158.         return { 
    159.             openTheWindow: true 
    160.         }; 
    161.     }, 
    162.     //開始給子組件一個回調(diào)函數(shù),用來做子組件給父組件通信使用 
    163.     buttonResponse:function(windowSatus){ 
    164.         this.setState({openTheWindow : windowSatus}); 
    165.     }, 
    166.     //開始渲染 
    167.     render: function () { 
    168.         console.log(jsonObj) 
    169.         return ( 
    170.             
       
    171.                  
    172.                  
    173.                      
    174.                     

       
    175.                      
    176.                          
    177.                      
    178.                  
    179.              
    180.         ); 
    181.     }, 
    182. //    執(zhí)行hook函數(shù):重新渲染完成的時候調(diào)這個函數(shù) 
    183.     componentDidUpdate:function(){ 
    184.         console.log(this.state.openTheWindow); 
    185.     } 
    186. }); 
    187. ReactDOM.render( 
    188.     
    189.     document.getElementById('container') 
    190. );  

    ***就是一個很重要的問題:就是多層級的據(jù)數(shù)據(jù)傳輸,如果還用這個方式來傳播的話,效率貌似是個大問題,解決辦法看大家的做法目前暫時還是flux之類的其他框架,等研究出來單獨寫篇文章吧


    當(dāng)前文章:ReactJS組件之間如何進(jìn)行通信
    URL地址:http://m.5511xx.com/article/dhgspgd.html

    其他資訊