【发布时间】:2020-01-15 06:48:04
【问题描述】:
我无法获得正确的 redux 存储/状态形状。
我有 2 个用于图像和视频的 reducer 函数,如下所示 -
imageReducer.js -
const initialState = {
images:{
imageArray:[],
selectedImage:{}
}
}
export default function (state = initialState,action) {
switch(action.type) {
case types.FLICKR_IMAGES_SUCCESS:
return {
...state,
images: {
...state.images.imageArray,...action.images
}
}
case types.SELECTED_IMAGE:
return {
...state,
images: {
...state.images.selectedImage,...action.image
}
}
default:
return state;
}
}
videoReducer.js -
const initialState = {
videos:{
videoArray:[],
selectedVideo:{}
}
}
export default function (state = initialState,action) {
switch(action.type) {
case types.SHUTTER_VIDEOS_SUCCESS:
return {
...state,
videos: {
...state.videos.videoArray,...action.videos
}
}
case types.SELECTED_VIDEO:
return {
...state,
images: {
...state.videos.selectedVideo,...action.video
}
}
default:
return state;
}
}
根减速器 -
import { combineReducers } from 'redux';
import img from './imageReducer';
import vid from './videoReducer';
// Combines all reducers to a single reducer function
const rootReducer = combineReducers({
img,
vid
});
我得到的最终 redux 状态与 initialState 的形状不匹配,我希望我的最终状态形状与传递给减速器函数的 initialState 一样
【问题讨论】:
标签: reactjs redux react-redux