Redux Toolkit slice migration and legacy reducer compatibility
20K reputation · 25 Dec 2024, 18:13 UTC
Transitioning to createSlice Architecture
Migrating a legacy Redux codebase to Redux Toolkit (RTK) involves shifting from manual action-type constants and switch-case reducers to the createSlice API. While RTK integrates Immer to allow mutating-style logic, legacy reducers still require strict immutability to prevent state corruption.
A technical uncertainty arises when a single store maintains a hybrid state tree containing both legacy reducers and RTK slices. Because Immer's behavior is scoped specifically to createSlice and createReducer, the consistency of state updates across the store depends on the strict separation of these logic patterns.
- Legacy reducers must return new state objects.
- RTK slices handle the immutable conversion internally.
Does the Redux store maintain predictable state transitions when a legacy reducer and an RTK slice modify overlapping segments of the state tree? What is the recommended strategy for ensuring state integrity during a phased migration where both patterns coexist?