有一个可用于自动聚焦的属性
autoFocus,我们可以使用该属性来自动聚焦输入元素而不是使用
ref。
autoFocus与输入元素一起使用:
<input autoFocus />
我们也可以使用
ref,但是使用ref时,我们需要在正确的位置调用focus方法,您在
componentWillUpdate生命周期方法中调用该方法,该方法在初始渲染期间不会触发,而不是使用
componentDidMountlifecycle方法:
componentDidMount(){ this.focus();}shouldComponentUpdate
:始终在render方法之前调用,并且可以定义是否需要重新渲染或可以跳过。显然,在初始渲染时永远不会调用此方法。仅当组件中发生某些状态更改时,才会调用它。
shouldComponentUpdate返回的true会立即调用
componentWillUpdate 。
componentDidMount
:一旦执行了render方法,就会调用componentDidMount函数。可以使用此方法访问DOM,从而可以定义DOM操作或数据获取操作。任何DOM交互都应始终在其中进行。
参考:https :
//facebook.github.io/react/docs/react-
component.html



