esamattis / immer-reducer

Type-safe and terse reducers with Typescript for React Hooks and Redux
http://npm.im/immer-reducer
MIT License
224 stars 15 forks source link

Replacing draftState completely #37

Closed skoshy closed 4 years ago

skoshy commented 5 years ago

Is there a way to replace the draftState with a new state? See the immer Readme for an example

Key line is here:

It is also allowed to return arbitrarily other data from the producer function. But only if you didn't modify the draft. This can be useful to produce an entirely new state.

Can the same be done with functions in an ImmerReducer? My use case is to completely reset the state back to the original state. The alternative I'm doing is looping through all the keys of this.draftState and setting those equal to the initial state.

esamattis commented 5 years ago

Just added this in a recent release

You can do this.draftState = newState which is the same as returning new state from the immer produce-function.

Should add this bit to README...