Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

为什么在es6在这种情况下不需要bind this

这种情况为什么不需要bind this?

class ArticleForm extends React.Component {
  // ...
  onCancelImageDialog = () => {
    this.setState({
      imageDialogOpen: false
    })
  }

  render() {
    return <Child onClick={this.onCancelImageDialog} />
  }
}

这种情况需要

class ArticleForm extends React.Component {
  // ...
  onCancelImageDialog() {
    this.setState({
      imageDialogOpen: false
    })
  }
  render() {
    return <Child onClick={this.onCancelImageDialog.bind(this)} />
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

()=>{} 这种形式的代码,语法规定就是(function(){}).bind(this),即自动添加了bind this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...