reactjs是一枚新進小鮮肉,跟gulp搭配流行一段時間了。工作或者面試中經常遇到這樣的問題,“子組件如何向父組件傳值?”。其實很簡單,概括起來就是:react中state改變了,組件才會update。父寫好state和處理該state的函數,同時將函數名通過props屬性值的形式傳入子,子調用父的函數,同時引起state變化。子組件要寫在父組件之前。具體寫法看下面3個例子。
例子1.這里如下圖,用戶郵箱為父,綠色框為子。 父組件為用戶輸入的郵箱設好state,即“{email: ''}”,同時寫好處理state的函數,即“handleEmail”,這兩個名稱隨意起;再將函數以props的形式傳到子組件,子組件只需在事件發生時,調用父組件傳過來的函數即可。
//以下所有例子對應的html <body> <div id="test"></div> </body>
//子組件 var Child = React.createClass({ render: function(){ return ( <div> 請輸入郵箱:<input onChange={this.props.handleEmail}/> </div> ) } }); //父組件,此處通過event.target.value獲取子組件的值 var Parent = React.createClass({ getInitialState: function(){ return { email: '' } }, handleEmail: function(event){ this.setState({email: event.target.value}); }, render: function(){ return ( <div> <div>用戶郵箱:{this.state.email}</div> <Child name="email" handleEmail={this.handleEmail.bind(this)}/> </div> ) } }); React.render( <Parent />, document.getElementById('test') );
例子2.有時候往往需要對數據做處理,再傳給父組件,比如過濾或者自動補全等等,下面的例子對用戶輸入的郵箱做簡單驗證,自動過濾非數字、字母和"@."以外的字符。
//子組件,handleVal函數處理用戶輸入的字符,再傳給父組件的handelEmail函數 var Child = React.createClass({ handleVal: function() { var val = this.refs.emailDom.value; val = val.replace(/[^0-9|a-z|\@|\.]/ig,""); this.props.handleEmail(val); }, render: function(){ return ( <div> 請輸入郵箱:<input ref="emailDom" onChange={this.handleVal}/> </div> ) } }); //父組件,通過handleEmail接受到的參數,即子組件的值 var Parent = React.createClass({ getInitialState: function(){ return { email: '' } }, handleEmail: function(val){ this.setState({email: val}); }, render: function(){ return ( <div> <div>用戶郵箱:{this.state.email}</div> <Child name="email" handleEmail={this.handleEmail.bind(this)}/> </div> ) } }); React.render( <Parent />, document.getElementById('test') );
例子3.如果還存在孫子組件的情況呢?如下圖,黑框為父,綠框為子,紅框為孫,要求子孫的數據都傳給爺爺。原理一樣的,只是父要將爺爺對孫子的處理函數直接傳下去。
//孫子,將下拉選項的值傳給爺爺 var Grandson = React.createClass({ render: function(){ return ( <div>性別: <select onChange={this.props.handleSelect}> <option value="男">男</option> <option value="女">女</option> </select> </div> ) } }); //子,將用戶輸入的姓名傳給爹 //對於孫子的處理函數,父只需用props傳下去即可 var Child = React.createClass({ render: function(){ return ( <div> 姓名:<input onChange={this.props.handleVal}/> <Grandson handleSelect={this.props.handleSelect}/> </div> ) } }); //父組件,准備了兩個state,username和sex用來接收子孫傳過來的值,對應兩個函數handleVal和handleSelect var Parent = React.createClass({ getInitialState: function(){ return { username: '', sex: '' } }, handleVal: function(event){ this.setState({username: event.target.value}); }, handleSelect: function(event) { this.setState({sex: event.target.value}); }, render: function(){ return ( <div> <div>用戶姓名:{this.state.username}</div> <div>用戶性別:{this.state.sex}</div> <Child handleVal={this.handleVal.bind(this)} handleSelect={this.handleSelect.bind(this)}/> </div> ) } }); React.render( <Parent />, document.getElementById('test') );