Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add on effect hook #59

Merged
merged 5 commits into from
Aug 31, 2016
Merged

Add on effect hook #59

merged 5 commits into from
Aug 31, 2016

Conversation

sorrycc
Copy link
Member

@sorrycc sorrycc commented Aug 31, 2016

比如:实现 effect 执行时,自动切换 loading 状态。

定义插件:

const SHOW = '@@LOADING/SHOW';
const HIDE = '@@LOADING/HIDE';

function autoLoading() {
  return {
    extraReducers: {
      loading(state, action) {
        switch (action.type) {
          case SHOW:
            return true;
          case HIDE:
            return false;
          default:
            return false;
        }
      },
    },
    onEffect(effect, { put }, model) {
      modelNamespace = model.namespace;
      return function*(...args) {
        count = count * 2;
        yield put({ type: SHOW });
        yield effect(...args);
        yield put({ type: HIDE });
      };
    },
  };
}

使用:

const app = dva();
app.use(autoLoading());

然后在 state.loading 就可以自动切换状态了。

@sorrycc sorrycc merged commit 943a435 into master Aug 31, 2016
@sorrycc sorrycc deleted the add-onEffect-hook branch August 31, 2016 09:19
@sorrycc
Copy link
Member Author

sorrycc commented Aug 31, 2016

dvajs/dva-hackernews@2c3330b
应用 dva-loading 在 dva-hackernews 里,删了好多代码。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant