有没有其他优雅的方式来做功能绑定?

时间:2015-09-16 11:32:17

标签: javascript reactjs bind ecmascript-6 flux

我为我的项目使用ES6类语法,React.js和Flux。 这是代码的一部分:

export default class Splash extends React.Component {

  constructor() {
    super();
    this.state = Comm.reqSplash();
  }

  componentDidMount(){
    this._appReadyBound = this._appReady.bind(this);
    SplashStore.subscribe(this._appReadyBound);
  }

  //Trigger when app data is loaded, if splash is in non-intro mode then end splash
  _appReady(){
    SplashStore.unsubscribe(this._appReadyBound);
    //If intro mode, do nothing
    if (this.state.mode !== "non-intro") return;
    this.endSplash();
  }
}

正如您所见,在" componentDidMount"方法,我必须创建一个绑定版本的" _appReady"方法

如果我不与"这"," _appReady"方法不会表现得正常。如果我没有制作绑定版本,则取消订阅方法与" removeChangeListener"相同。如果你对它更熟悉,就不会做它的工作,这意味着听众仍然在听众名单上。

所以我想知道是否有一种优雅的方式来进行绑定,或者避免绑定。或许我应该放弃ES6类语法?

1 个答案:

答案 0 :(得分:1)

你试过吗

_appready = () => {
  //function here

}

希望有所帮助