-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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
Could “ plain” class instances be stored in Redux store state. #4649
Comments
i wouldn't recommend taking a comment from 2016 as gospel 😛 (we haven't recommended usage of ImmutableJS for a long time) for the purposes of devtools and persistence, anything you can't do |
@EskiMojo14 |
A POJO is something like class Foo {
foo = "bar"
}
console.log(JSON.parse(JSON.stringify(new Foo())) instanceof Foo) // false |
@EskiMojo14 I am still unclear, how to declare POJO objects in Typescript? |
like i said, you make typically plain javascript object with the object literal syntax. no class keyword at all |
interface Person {
firstname: string
lastname: string
}
const myPersonThatIsNotAClass: Person = {
firstname: "foo",
lastname: "bar"
} Really... just use objects. Classes are not a "TypeScript feature", classes are part of JavaScript since 2015, and TypeScript doesn't add anything on top. (Also see the accepted answer to the question you linked to. It doesn't recommend classes.) |
Thanks, @phryneas . I will use interfaces instead of classes. |
The guidance is still accurate: you still should not put any class instance of any kind into Redux state. Technically speaking, the code will run. But, classes are not meant for immutable updates, and they won't serialize properly. |
@markerikson , should the rule not only say what not to do, but suggest how to do it correctly? |
@michael-freidgeim-webjet the docs are pretty clear that you should only put plain JS objects/arrays/primitives into state:
|
@markerikson , sorry, but neither of 4 you provided links discuss “non-serializable” values. I still believe that in the rule it is not obvious that the class instances are non-serializable and they can’t be considered as POJOs.(I am a full stack developer with more experience in C#, rather than JS) I am fully understand that “Style Guide" is a concise summary of approaches, but it will be good to provide hyper-links that allow to clarify/explain statements of the rule. Other rules have “Detailed Explanation” section. It’s just my feedback as a reader, it’s up to you to decide is it beneficial for other readers |
@michael-freidgeim-webjet I'm curious, what specific information would you like to see added / were you expecting to see? Something specifically saying "class instances aren't serializable", something saying why class instances aren't serializable, or something else? |
@markerikson I suggest something like the following:
I’ve provided links, that I found today to understand the question, but you may find better references |
I will repeat the point I made earlier, because I think you might be coming from another programming language and be missing a very central point about the programming language you are currently using: Classes are not a TypeScript featureClasses are a JavaScript feature. Nothing about classes is specific to TypeScript. TypeScript only adds type information in your editor, but it doesn't add features to your programming language - you are programming in JavaScript. JavaScript has classes If you want to store data, use an object or an array. If you want data with attached logic, use a class. Storing data without attached logic in a class is a misuse of features of your programming language - this has nothing to do with Redux and everything with learning the programming language you are using. |
@markerikson you may also add a reference from section https://redux.js.org/style-guide/#do-not-put-non-serializable-values-in-state-or-actions to another rule below |
Section https://redux.js.org/style-guide/#do-not-put-non-serializable-values-in-state-or-actions has a statement
“Avoid putting non-serializable values such as Promises, Symbols, Maps/Sets, functions, or class instances into the Redux store state”
“class instances” sounds more restrictive that it should be. I have a collection of typescript class instances, that has properties as primitives or other smaller typescript classes. The classes have data fields, but not behavior(such an events).
According to
#1407 (comment)
“we recommend that both actions and the state are plain objects (or something like Immutable Maps)”
( referenced from
https://redux.js.org/faq/organizing-state#can-i-put-functions-promises-or-other-non-serializable-items-in-my-store-state)
Could you please add a clarification in the rule, which class instances (or maps) are allowed and which are not recommended .
Currently it sounds, that I have to destruct all properties of my classes and store them as separate arrays, which defeat the purpose of having classes as not trivial data structures.
The text was updated successfully, but these errors were encountered: