简体   繁体   中英

How to change reducer to create slice in redux toolkit

 import { combineReducers } from "redux";
 const initialState = {
     sidebarShow: true,
  }
   const changeState = (state = initialState, { type, ...rest }) => {
     switch (type) {
       case 'set':
        return { ...state, ...rest }
      default:
         return state
   }
  }
   export const rootReducer = combineReducers({
     nav: changeState
   })

1.how to change this code to createslice format in redux toolkit

you need to follow Redux Toolkit

  import { createSlice } from '@reduxjs/toolkit'
    
 const initialState = {
     sidebarShow: true,
  }
    
    export const counterSlice = createSlice({
      name: 'changeState',
      initialState,
      reducers: {
        set: (state, action) => {
        state = {...state , action.payload}
},

      },
    })
    export const { set } = counterSlice.actions
    
    export default counterSlice.reducer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM