用redux解释curried连接调用

时间:2016-03-29 05:47:45

标签: redux

我在ng-redux示例中查看this code

class AppController {
  constructor($ngRedux, $scope, AsyncActions) {
    const unsubscribe = $ngRedux.connect(this.mapStateToThis, AsyncActions)((selectedState, actions) => {
      this.componentWillReceiveStateAndActions(selectedState, actions);
      Object.assign(this, selectedState, actions);
    });
    this.options = ['angularjs', 'frontend'];
    this.handleChange = this.handleChange.bind(this);
    this.handleRefreshClick = this.handleRefreshClick.bind(this);

    this.fetchPostsIfNeeded(this.selectedReddit);
  }

  componentWillReceiveStateAndActions(nextState, nextActions) {
    if (nextState.selectedReddit !== this.selectedReddit) {
      nextActions.fetchPostsIfNeeded(nextState.selectedReddit);
    }
  }

从初始连接调用返回的函数调用函数的目的是什么?

这是在redux文档中吗?

1 个答案:

答案 0 :(得分:0)

通常,currying用于缓存一些中间结果。在某些情况下,它会使您的代码更加优化。

在此示例中,它允许您创建一次连接,然后随时订阅/取消订阅。

class AppController {
  constructor($ngRedux, $scope, AsyncActions) {
    this.handler = $ngRedux.connect(this.mapStateToThis, AsyncActions);
  }

  activate() {
    this.removeHandler = this.handler(this.onUpdate)
  }

  deactivate() {
    this.removeHandler();
    this.removeHandler = null;
  }

  onUpdate() {
  }
}