Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compare original state with reference equality #829

Closed
jackielii opened this issue Dec 1, 2020 · 5 comments · Fixed by #832
Closed

compare original state with reference equality #829

jackielii opened this issue Dec 1, 2020 · 5 comments · Fixed by #832
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@jackielii
Copy link
Contributor

jackielii commented Dec 1, 2020

I have a reducer created using createSlice. Inside one of the case reducer, I'd like to compare with the original state with reference equality. How do I achieve this?

const slice = createSlice({
  name: 'filter',
  initialState: emptyConstraint,
  reducers: {
    addPlain(state, action: PayloadAction<{ parent: Constraint.AsObject }>) {
      if (state === action.payload.parent) { // <<<< how should I do equality check? do I need to import: https://immerjs.github.io/immer/docs/original
          // do something with state
      }
    },
})
@phryneas
Copy link
Member

phryneas commented Dec 1, 2020

You can use original from immer:

import {original] from 'immer';

if (original(state) === action.payload.parent)

@jackielii
Copy link
Contributor Author

jackielii commented Dec 1, 2020

You can use original from immer:

This is what I end up doing, however, I had to add immer as an explicit dependency as it's not re-exported. Would this be ok? Will there be any incompatibilities?

@jackielii
Copy link
Contributor Author

jackielii commented Dec 1, 2020

@phryneas turns out it doesn't always work.

If I just do yarn add immer, it installs 8.0.0. original calls isDraft which will return false because the draft from redux-toolkit is different from immer

Did found a workaround: In Yarn, I have to align the immer version to exactly redux-toolkit is using so that yarn resolves them to the same immer source code inside node_modules.

However, the workaround is flaky and since redux-toolkit re-exports some of the immer's functions like current, I think we should export original as well.

@jackielii jackielii reopened this Dec 1, 2020
@phryneas
Copy link
Member

phryneas commented Dec 1, 2020

The current RTK 1.5 uses immer 8.0.0. But yeah, probably we should re-export original as it is quite useful. Actually, I thought we already did that. @markerikson what are your thoughts? We had good uses for original in performance scenarios if I recall correctly

@markerikson
Copy link
Collaborator

Probably reasonable to re-export that too, yeah.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants