reactjs - 将状态值从 redux reducer 传递到另一个 reducer
问题描述
我想将redux状态的值从reducer传递给另一个reducer。就我而言,我想将groups
from state的值传递groupReducer.js
给 to scheduleReducer.js
。我正在使用combineReducers
fromredux
在它们之间进行组合。
这是我的代码:
groupReducer.js
...
const initialState = {
groups: [],
...
export default function(state = initialState, action) {
switch (action.type) {
case FETCH_GROUPS:
return {
...state,
groups: action.payload
};
...
scheduleReducer.js
const initialState = {
...
}
...
export default function(state = initialState, action) {
switch (action.type) {
case GROUP_INFO:
return {
group: groupInfo(action.payload.id, SHOULD_PASS_GROUPS_FETCHED_FROM_GROUPREDUCER)
};
我想将它传递groups
给最后一个减速器,我该怎么做?
解决方案
您可以使用 thunk 访问完整的state
对象。从中获取组groupReducer
,然后调用您的操作SHOULD_PASS_GROUPS_FETCHED_FROM_GROUPREDUCER
将这些组传递给sheduleReducer
.
// thunk action creator, use it as a normal action creator //
// while dispatching //
function passGroupFetchedFromGroupReducer() {
return function (dispatch, getState) {
// Get state object from getState(). Try console.log(getState() to get
// idea of the shape of what getState() returns.
const groupsToPass = getState().groupReducer.groups;
// Then dispatch your action with the payload
dispatch({
type: 'SHOULD_PASS_GROUPS_FETCHED_FROM_GROUPREDUCER',
payload: groupsToPass
})
};
}
// scheduleReducer.js //
const initialState = {
groups: [],
...
}
...
export default function(state = initialState, action) {
switch (action.type) {
case GROUP_INFO:
return {
...state,
groups: action.payload
};
}
推荐阅读
- python - 芹菜和pyinstaller
- jooq - Does `ResultQuery#fetchAsync()` work with r2dbc in jOOQ 3.15.1?
- arrays - Excel过滤值到数组
- wordpress - 我们如何创建一个自定义模块,它有一个 cron 钩子,它调用 API 并使用 wordpress 创建/更新
- python - 如何制作两个分组列的组合直方图?
- docusignapi - 向 Docusign 中的所有文档添加选项卡
- git - github - 降级后无法解决冲突和合并
- excel - VBA在满足条件时将特定单元格从一个工作表复制到另一个工作表
- c# - 如何使用/何时使用可空类型打开类型?
- python - Matplotlib 颜色条未显示 2 个离散值的标签