redux-state-sync icon indicating copy to clipboard operation
redux-state-sync copied to clipboard

Configuration with redux-toolkit

Open michaelkoelle opened this issue 3 years ago • 4 comments

How do i configure immutability with redux-toolkit which uses ImmerJS?

michaelkoelle avatar Dec 21 '21 15:12 michaelkoelle

Hi @koellemichael, I haven't tried using redux-state-sync with ImmerJS yet, will try setup an example when I got time. PR is welcome, if you wanna try setup an example for redux-toolkit

aohua avatar Feb 09 '22 09:02 aohua

@aohua any updates on this?

untitledlt avatar Jun 02 '22 21:06 untitledlt

I found a workaround for now. I think it might actually be a problem on the redux-toolkit side. Somehow redux-toolkit uses the wrong identifier if the reducers are written like this:

reducers: {
    booksAddOne: adapter.addOne,
    ...
  },

I also tried to specify the identifier using the selectId option in the adapter, but still didn't work. However, if you extract the state and action and pass it explicitly it works:

reducers: {
    booksAddOne: (state, action) => adapter.addOne(state, action.payload),
    ...
  },

Hope that it works for you, too!

michaelkoelle avatar Jun 02 '22 21:06 michaelkoelle

I found a workaround for now. I think it might actually be a problem on the redux-toolkit side. Somehow redux-toolkit uses the wrong identifier if the reducers are written like this:

reducers: {
    booksAddOne: adapter.addOne,
    ...
  },

I also tried to specify the identifier using the selectId option in the adapter, but still didn't work. However, if you extract the state and action and pass it explicitly it works:

reducers: {
    booksAddOne: (state, action) => adapter.addOne(state, action.payload),
    ...
  },

Hope that it works for you, too!

For anyone who came across this, this is due to redux toolkit's adapter checking whether the action is FSA or not (https://github.com/reduxjs/redux-toolkit/blob/e45425128d3c3168c7daa71e5f38f5151234cb8d/packages/toolkit/src/entities/state_adapter.ts#L34 ).

yqkqknct avatar Aug 01 '23 03:08 yqkqknct