简体   繁体   English

如何跨不同的减速器处理 state 以触发反应 redux 中的通知

[英]How to handle state across different reducers for triggering notification in react redux

How do i update root state on store from one reducer to another reducer if some message notification triggers.如果某些消息通知触发,我如何将存储中的根 state 从一个减速器更新到另一个减速器。 I want to change message state of store when user login happens and wants to show that message on top level MESSAGE component.当用户登录并希望在顶级 MESSAGE 组件上显示该消息时,我想更改商店的消息 state。 I want to change message state from userReducer on dispatching LOGIN_FAILURE action.我想在调度 LOGIN_FAILURE 操作时更改来自 userReducer 的消息 state。 But it is updating message in initialState.user.messages itself but i want to update in initialState.messages not in initialState.user.messages.但它正在更新 initialState.user.messages 本身的消息,但我想在 initialState.messages 中更新,而不是在 initialState.user.messages 中更新。

How can i do this?我怎样才能做到这一点?

Dispatching LOGIN_FAILURE actions.调度 LOGIN_FAILURE 操作。

initialState.js初始状态.js

在此处输入图像描述

rootReducer.js used in createStore createStore 中使用的 rootReducer.js

在此处输入图像描述

userReducer.js userReducer.js

在此处输入图像描述

messageReducer.js messageReducer.js

在此处输入图像描述

App.js in which MESSAGE component is used使用 MESSAGE 组件的 App.js

在此处输入图像描述

As you may know each reducer has its own state, so for example for userReducer you are using the initialState , so您可能知道每个减速器都有自己的 state,例如对于userReducer ,您使用的是initialState ,所以

const initialState = {
  user: {},
  messages: []
};

the initial state above, it's used into userReducer only, so it is namespace for user only instead of be sharing cross reducer, so whatever change you do into userReducer will affect only its state I mean user: {} , in the case that you want to update messages for messageReducer , so should move your logic for LOGIN_FAILURE to the reducer that is intended to affect, so if for example yo need to listen for LOGIN_FAILURE into userReducer too, you can do it, but this should be done only if you need to affect something related to the user state instead of message state.上面的初始 state 仅用于 userReducer,因此它仅用于用户的命名空间,而不是共享交叉减速器,因此您对userReducer所做的任何更改都只会影响其 state 我的意思是user: {} ,如果你想要更新messageReducermessages ,因此应该将LOGIN_FAILURE的逻辑移动到打算影响的 reducer,因此,例如,如果您也需要在userReducer LOGIN_FAILURE您可以这样做,但只有在您需要时才应该这样做影响与用户 state 相关的内容,而不是消息 state。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM