React 支持一种非常特殊的属性 Ref ,可以用来绑定到 render() 输出的任何组件上
import React,{ Component } from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import * as serviceWorker from './serviceWorker'; class Focus extends Component{ constructor(props){ super(props); this.focusFn=this.focusFn.bind(this); } focusFn(){ .refs.myInput.focus(); } render(){ return( <div> <input type="text" ref="myInput" /> <br/> <button onClick={this.focusFn}>点我</button> </div> ) } } ReactDOM.render( <div> <Focus /> </div>,document.getElementById('example') ); serviceWorker.unregister();