Blog>
Snippets

Combining Multiple Slices with combineSlices

Show how to combine multiple slices into a single root reducer using Redux Toolkit 2.0's `combineSlices` feature.
import { configureStore, createSlice, combineSlices } from '@reduxjs/toolkit';

// Define a slice for user-related state
const userSlice = createSlice({
  name: 'user',
  initialState: { name: '', email: '' },
  reducers: {
    // ...define user-related reducers
  }
});

// Define a slice for product-related state
const productSlice = createSlice({
  name: 'product',
  initialState: { items: [] },
  reducers: {
    // ...define product-related reducers
  }
});

// Combine the slices using combineSlices to create a single root reducer
const rootReducer = combineSlices({
  user: userSlice.reducer,
  product: productSlice.reducer
});

// Configure the store with the combined reducer
const store = configureStore({
  reducer: rootReducer
});
This code snippet demonstrates how to define and combine multiple slices into a single root reducer using `createSlice` and `combineSlices` from Redux Toolkit, and then configure the Redux store with the combined reducer.