我先贴上代码,然后说说我自己的理解,请高手帮我指正一下
class AutoFocusInput extends Component {
componentDidMount () {
this.input.focus()
}
render () {
return (
<input ref={(input) => this.input = input} />
)
}
}
ReactDOM.render(
<AutoFocusInput />,
document.getElementById('root')
)
<input ref={(input) => this.input = input} />这段代码,我实在不理解this指代的是什么,是ref所在的input标签?还是class AutoFocusInput
这段代码<input ref={(input) => this.input = input} />翻译过来就是
<input ref={function(input) ={return this.input = input} />
就是让this.input=input,这里的this就是指代的input标签吧,
假如我有很多个这种input标签,this的含义就是指我当前选择的这个input表现。
不知道我以上的理解到底对不对,谢谢
慕仙森
富国沪深
小怪兽爱吃肉
相关分类