question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Couldn't work with 'immerable' custom Class

See original GitHub issue

It seems that there are few problems with immerable Classes.

Imagine following class

import { immerable } from 'immer';

export class DocLoading {
  [immerable] = true;
  constructor(data = {}) {
    this.documentId = data.documentId;
  }
  setDocumentId(docId) {
    this.documentId = docId;
  }
}

This class is used as initialState for the following reducer

createSlice({
  name: 'documentLoading',
  initialState: new DocLoading(),
  reducers: {
    setDocumentId(state, { payload }) {
      state.setDocumentId(payload);
    }
  }
})

I see two issues:

  • I have to turn serializableCheck off
  • when i try to dispatch following action
dispatch({ type: 'documentLoading/setDocumentId', payload: 'documentId1' });

I have got an error from ImmutableStateInvariantMiddleware A state mutation was detected inside a dispatch, in the path: 'documentViewer.documentLoadingMetaData.documentId'. Take a look at the reducer(s) handling the action {"type":"documentViewer/setDocumentId","payload":"doc123"}. (http://redux.js.org/docs/Troubleshooting.html#never-mutate-reducer-arguments)

Please let me know if I’m doing something wrong or let me few tips how to deal with immerable custom Classes in RTK.

Thx

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Aldeeecommented, Apr 10, 2020

@phryneas you’ve provided valid example. It seems that I had an issue with redux-devtools while trying dispatch an action within it. Thx for clarification. Code works as expected in your’s experiment.

@markerikson could you please explain what is the best place within RTK to use Data Models (ES Classes)? I would like to use it like in the example above. But also I could do this by using in the selector. Is it ok, or could you suggest better place?

const getDocumentLoading = (state) => state.documentLoading;
const documentLoadingSelector = createSelector(
  getDocumentLoading,
  (documentLoading) => new DocLoading(documentLoading)
)
1reaction
Aldeeecommented, Apr 10, 2020

@phryneas thx for you reply, I meant createSlice, updated in the first post. I will provide an example a bit later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't an immutable class have setter methods? [duplicate]
The above class is immutable because: String is immutable. The field 'str' is private and can't be altered.
Read more >
Immutability in C# - CODE Magazine
When an immutable object instance is created, the private _lock field is set to true . One of the requirements is to keep...
Read more >
hashing breaks for custom objects with "Invalid value used as ...
js introduce a weird bug when hashing custom objects, more specifically, a class with valueOf() overridden. The error being thrown is Uncaught TypeError: ......
Read more >
Make classes immutable or compatible with standard containers
So make your classes mutable and have them behave as proper values. ... And I couldn't change the command API which looked like...
Read more >
How To Create an Immutable Class in Java - DigitalOcean
Creating an Immutable Class in Java · Declare the class as final so it can't be extended. · Make all of the fields...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found