Skip to content

Initial createReducer state is not frozen #939

@markerikson

Description

@markerikson

Immer auto-freezes state when processed in a case reducer. However, we currently do not freeze the initial state. This means that it is possible to mutate the initial state before any actions are dispatched. Not sure if the mutation checker picks up on it or not.

Another issue is that I'm looking at updating the immutability and serialization checks to skip past frozen values for speed. If the initial state isn't frozen, we don't get the benefit there. Now, it's also likely that the initial state is almost empty so it won't take long to scan those values anyway, but I can't see a reason to not freeze the initial state.

Fortunately, it looks like it's trivial to do this by adding:

const frozenInitialState = createNextState(initialState, draft => {})

and using the frozen state instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions