0

I have a problem with reducer created by immer.js in TypeScript.

I don't understand the following:

When I use an object literal {} in initial state immutability works fine but once

const byId = (state = {}, action) =>
    produce(state, draft => {
        switch (action.type) {
            case RECEIVE_PRODUCTS:
               ...
)
        }
    })

I change literal to instance of object immutability stop works

const byId = (state = new SomeObject(), action) =>
    produce(state, draft => {
        switch (action.type) {
            case RECEIVE_PRODUCTS:
               ...
)
        }
    })

Can you someone idea why?

Thanks..

TomasB
  • 11
  • 4

2 Answers2

0

I have created parent object

export class Immerable {
  public [immerable] = true;
}

and all classes which I need to use within produce function has this parent. Thanks.

TomasB
  • 11
  • 4
-1

You typically want the first argument to produce to be a plain object or array.

Optionally it could be an "immerable class", though I'm not familiar with what that would be specifically (sounds like an internal thing to me) but it's not likely what your new SomeObject() call returns.

https://github.com/immerjs/immer/blob/master/src/immer.js#L61

If the first argument to produce is not one of these then it will not be "drafted".

https://github.com/immerjs/immer/blob/master/src/common.js#L20

lecstor
  • 5,619
  • 21
  • 27