从render方法中的嵌套函数中反映调用组件的方法

wkl*_*klm 3 javascript reactjs react-jsx react-native

我想在onCLick上调用updateCurrentThread方法,但是我遇到了以下错误:

未捕获的TypeError:无法读取未定义的属性"updateCurrentThread"

updateCurrentThread: function(thread) {
  this.setState({
  currentThread: thread
 }).bind(this);
},


render: function () {
  var threads = this.state.data.map(function (thread) {
    var boundClick = this.updateCurrentThread.bind(this, i);
    let time = getThreadDate(thread.timestamp);
    return (
      <div className="row thread" key={thread.threadID}>
        <ThreadParticipants onClick={boundClick} key={i} className="small-2 small-centered columns" ids={thread.participantIDs}/>
      </div>
  );
})
Run Code Online (Sandbox Code Playgroud)

ken*_*n4z 7

this自己的函数中的作用范围是功能,而不是组件.如果您能够使用词汇范围的ES6箭头函数,那么您当前的方法将起作用.在箭头函数之前,人们会将组件存储this在变量中(例如var self = this)然后self.updateCurrentThread.