https://react-cn.github.io/react/docs/more-about-refs.htmljavascript
組件的Refs用來獲取原生的HTML節點 html
<input id= "submitButton" type= "button" value="submit" onClick={this.changerUserInfo.bind(this, 99)}/>
點擊按鈕後 ,按鈕字體變紅 使用findDOMNode 操做java
//建立事件 changerUserInfo(age) { this.setState({age : age}); //第一種方式 var mySubmitButton = document.getElementById('submitButton'); ReactDOM.findDOMNode(mySubmitButton).style.color = 'red'; console.log('a'); }
<input id="submitButton" ref="submitButton" type= "button" value="submit" onClick= {this.changerUserInfo.bind(this, 99)}/>
//建立事件 changerUserInfo(age) { this.setState({age : age}); console.log(this.refs.submitButton); } // ==> <input type= "button" id = "submitButton" value= submit/>
//建立事件 changerUserInfo(age) { this.setState({age : age}); console.log(this.refs.submitButton); this.refs.submitButton.style.color = "red"; }
這種方式更加好react