首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何连接reducers逻辑

如何连接reducers逻辑
EN

Stack Overflow用户
提问于 2019-10-06 04:11:31
回答 1查看 28关注 0票数 1
代码语言:javascript
复制
const authReducer = createReducer(
  initialState,
  on(startLogin, (state) => ({...state, isLogging: true})),
  on(loginSuccessful, (state) => ({...state /*do sth other */})),
  on(loginSuccessful, loginFailed, (state) => ({...state, isLogging: false}))
);

在上面的示例中,我在多个操作之间共享逻辑(isLogging属性)。但是,当loginSuccesfull操作被分派时,只会触发其中一个reducers。

可以连接共享逻辑,而不是编写:

代码语言:javascript
复制
const authReducer = createReducer(
  initialState,
  on(startLogin, (state) => ({...state, isLogging: true})),
  on(loginSuccessful, (state) => ({...state, isLogging: false /*do sth other */})),
  on(loginFailed, (state) => ({...state, isLogging: false}))
);
EN

回答 1

Stack Overflow用户

发布于 2019-10-06 18:54:04

这是不可能的(目前),只有相同动作类型的最后一个注册动作接收更新。

代码语言:javascript
复制
const authReducer = createReducer(
  initialState,
  on(startLogin, (state) => ({...state, isLogging: true})),
  on(loginSuccessful, (state) => ({...state /*do sth other */})),
  on(loginSuccessful, loginFailed, (state) => ({...state, isLogging: false})) // only this one will be triggered for loginSuccessful
);

我们刚刚合并了一个PR,以使您的用例成为可能,这可能会在下一个版本中“退出”。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58251951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档