react-boilerplate / react-boilerplate-typescript

⚠️ MOVED TO react-boilerplate/react-boilerplate-cra-template

Home Page:https://github.com/react-boilerplate/react-boilerplate-cra-template

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Should reducers clone initalState to guard against mutation?

lsanders opened this issue · comments

commented

Is your feature request related to a problem? Please describe.
It's easy to accidentally mutate the initialState in reducers, especially from tests.

Describe the solution you'd like
Guard against mutation for initialState.

Describe alternatives you've considered
I really wouldn't want to introduce immutablejs, but maybe just lodash cloneDeep in the reducer:


export const initialState: ContainerState = {
  someDeepObject: new Map<string, ISomeInterface>(),
};

function myReducer(
  state: ContainerState = cloneDeep(initialState),
  action: ContainerActions): ContainerState {
  switch (action.type) {

Any other ideas?

Additional context
Thanks for the typescript fork of the awesome boilerplate!

Can you give an example of how you mutate the state ? Because readonly interfaces are simply the best solution for not mutating the state in reducers. Very simple and neat solution.

Here it doesn't let me to update fields in reducer. Is this the problem? Or something else ?

Screen Shot 2019-05-06 at 21 20 59

Closing since no response and its unclear. Please open it back if you feel like