reactjs 合并两个具有不同缩减器的存储文件和存储创建?

oxiaedzo  于 2023-01-04  发布在  React
关注(0)|答案(1)|浏览(104)

我正在尝试合并两个具有不同reducer和store creations的store文件。我不明白如何将changeState reducer(用于createStore)与API reducer(用于configureStore)合并。
门店一:

import { createStore } from 'redux'

const initialState = {
  sidebarShow: true,
}

const changeState = (state = initialState, { type, ...rest }) => {
  switch (type) {
    case 'set':
      return { ...state, ...rest }
    default:
      return state
  }
}

const store = createStore(changeState)
export default store

店铺二:

import { configureStore } from '@reduxjs/toolkit';
import { apiSlice } from './api/apiSlice';

  
export const store = configureStore({
    reducer: {
        [apiSlice.reducerPath]: apiSlice.reducer,
    },
    middleware: getDefaultMiddleware => 
    getDefaultMiddleware().concat(apiSlice.middleware),
    devTools: true
  
})

我尝试过合并Reducer,但是发现在使用configureStore时它们会自动合并。我也尝试过创建切片,但是也不起作用。我是Redux的新手,任何帮助都会很好。

rsl1atfo

rsl1atfo1#

溶液:
存储文件:

import { configureStore } from '@reduxjs/toolkit';
import { apiSlice } from './api/apiSlice';

 const initialState = {
        sidebarShow: true,
    }
    
    const setSideBarState = (state = initialState, { type, ...rest }) => {
        switch (type) {
          case 'set':
            return { ...state, ...rest }
          default:
            return state
        }
      }
    
    export const store = configureStore({
        reducer: {
            [apiSlice.reducerPath]: apiSlice.reducer,
            setSideBarState: setSideBarState
        },
        middleware: getDefaultMiddleware =>
            getDefaultMiddleware().concat(apiSlice.middleware),
        devTools: true
    
    })

边栏文件:
变更
state.sidebarShow

state.setSideBarState.sidebarShow

相关问题