代码中的唯一警告是由于您没有扩展正确的类,而需要扩展
React.Component。
class App extends React.Component { constructor(props){ super(props); this.handleClick = this.handleClick.bind(this); } handleClick(){ if(this.myTextInput !=null) { this.myTextInput.focus(); } } render (){ return ( <div> <input type="text" ref={(ref) => this.myTextInput = ref} /> <input type="button" value="'Focus the text input" onClick={this.handleClick} /> </div> ); } } ReactDOM.render(<App />, document.getElementById('app'));<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script><div id="app"></div>


