【发布时间】:2020-03-14 03:38:47
【问题描述】:
我有两个仪表板,其中包含我想在 redux 中实现的相似数据和属性。见下文。
Dashboard 1 : {filters, widgets, custom}
Dashboard 2: {filters, widgets, custom}
我想像这样创建我的 redux 状态:
{
dashboards: {
dashboard1:{
filter:{ // filter related stuff},
widgets:{ // widget related state},
custom:{ // custom state}
},
dashboard2: {
filter:{ // filter related state},
widgets:{ // widget related state},
custom:{ // custom state}
}
},
auth: {// auth related stuff},
...// other state keys
}
为了实现这一点,我正在尝试将这样的组合减速器用于仪表板
// xyz.reducer.js
combineReducers({
filters: filterReducers,
widgets: widgetReducers,
custom: CustomReducers
})
现在我创建了一个仪表板减速器,每次用户导航到某个仪表板时都会调用它。我想根据dashboardID 分配这个状态。像 -
const dashboardReducer = (state = defaultState, action) => {
switch (action.type) {
case CREATE_DASHBOARD: {
const { payload } = action;
return {
...state,
[payload.dasboardId]: // all the combined reducer state here
};
}
default:
return state;
}
};
有人可以建议我如何实现这一目标吗?如果有人有更好的建议,我也愿意提出建议 维护这种状态结构的方式。
【问题讨论】:
-
如果过滤器和小部件是仪表板的属性,那么为什么它们有自己的减速器?还有什么是
CustomReducers?您是否需要同时访问两个仪表板?如果没有,您可以拥有一个减速器,然后根据您应该看到的仪表板将必要的数据传递给一个减速器。或者,您可以拥有一个具有dashboards属性的仪表板减速器。该键将指向一组仪表板,您可以根据 id 确定要使用的仪表板。 -
您的仪表板数量是动态的?还是只是用 2 个仪表板固定?
-
@DucHong 现在它是动态的基于用户访问。
标签: reactjs redux react-redux state react-state-management