我更喜欢使用 combineReducers 为减速器创建组。生病分享我的中间件设置,希望它对你有帮助!
store.config.js :
import rootReducer from '../_reducers'
export const history = createBrowserHistory()
export const store = configureStore();
export default function configureStore(preloadingState) {
const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
rootReducer(history),
preloadingState,
composeEnhancer(
applyMiddleware(
routerMiddleware(history),
thunk,
),
),
)
return store
}
index.js(在 reducers 文件夹中):
const rootReducer = (history) => combineReducers({
router: connectRouter(history),
single: combineReducers({
post: postReducer,
guide: guideReducer,
course: courseReducer,
lesson: lessonReducer,
exercise: exerciseReducer,
}),
bank: combineReducers({
posts: postsReducer,
guides: guidesReducer,
courses: coursesReducer,
lessons: lessonsReducer,
exercises: exercisesReducer,
}),
melodition: playerReducer,
user: combineReducers({
profile: profileReducer,
favorites: favoriteReducer,
}),
snackbar: snackbarReducer,
auth: authReducer,
});
export default rootReducer;
注意:我在一个带有connected-react-router的大型项目上这样做了,也许这对每个项目都没有好处。