猿问

React学习过程中遇到一句不理解的语法

我先贴上代码,然后说说我自己的理解,请高手帮我指正一下


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表现。

不知道我以上的理解到底对不对,谢谢


守候你守候我
浏览 414回答 3
3回答

慕仙森

jsx 最终 会编译成 最下下面的 形式, 这样 你在看 箭头函数 this 的绑定

富国沪深

这里的&nbsp;this&nbsp;指的是当前&nbsp;AutoFocusInput&nbsp;实例。

小怪兽爱吃肉

this指向的是当前这个组件
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答