Blog>
Snippets

Configuring Redux Store with Tree Shaking

Demonstrates how to configure a Redux store in a way that enables tree shaking for unused code elimination by setting up feature-specific slices.
import { configureStore } from '@reduxjs/toolkit';
import usersReducer from './features/users/usersSlice';
import productsReducer from './features/products/productsSlice';

// Configure Redux store
// Slices are split into feature-specific files to enable tree-shaking
const store = configureStore({
  reducer: {
    users: usersReducer,
    products: productsReducer
    // Add more feature slices as needed
  }
});

export default store;
This code imports configureStore from Redux Toolkit and two feature-specific slices: usersReducer and productsReducer. It then creates a Redux store using configureStore and attaches the slices to the store. This setup allows for tree-shaking because each slice is maintained in its own module, and only the used exports from those slices are included in the final bundle.