Couldn't work with 'immerable' custom Class
See original GitHub issueIt 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:
- Created 3 years ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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?
@phryneas thx for you reply, I meant
createSlice
, updated in the first post. I will provide an example a bit later.