Skip to content

Commit 010bcf0

Browse files
committed
Ensure initial state gets frozen
1 parent fd31cdc commit 010bcf0

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

packages/toolkit/src/createReducer.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,14 @@ export function createReducer<S extends NotFunction<any>>(
212212
? executeReducerBuilderCallback(mapOrBuilderCallback)
213213
: [mapOrBuilderCallback, actionMatchers, defaultCaseReducer]
214214

215-
const getInitialState = () =>
216-
createNextState(
217-
isStateFunction(initialState) ? initialState() : initialState,
218-
() => {}
219-
)
215+
// Ensure the initial state gets frozen either way
216+
let getInitialState: () => S
217+
if (isStateFunction(initialState)) {
218+
getInitialState = () => createNextState(initialState(), () => {})
219+
} else {
220+
const frozenInitialState = createNextState(initialState, () => {})
221+
getInitialState = () => frozenInitialState
222+
}
220223

221224
function reducer(state = getInitialState(), action: any): S {
222225
let caseReducers = [

0 commit comments

Comments
 (0)