Can I store an array of class instances in Redux?

Viewed 941

While running through one of Redux's tutorials, I ran across this warning: "Redux actions and state should only contain plain JS values like objects, arrays, and primitives. Don't put class instances, functions, or other non-serializable values into Redux!"

Does this mean that an array of class instances shouldn't be held in redux? For example, say I have a Chat app. Say I have a Conversation class that holds an array of participants, array of messages, an id, etc. Can I add to redux an array of Conversation instances?

If not, why? Should a React application's data model not rely much on classes and inheritance?

2 Answers

The plain JS values are simply a Plain Old JavaScript Objects (POJO):

var obj = {};// this is a POJO
var obj = new Object();// this is POJO

What is not a PJOJ, is something like this:

var Obj = function(welcome_text) {
  this.welcome_text = welcome_text;
}
var c = new Obj("Welllllcome!!!!");// this is not a POJO !

I folowed up the docs of redux, and they gived an example in this link. They explain that, for example, you can not have new Date() as a params for your actions/states, beacause the new Date() will return a non POJO ! Instead, you should do new Date().toISOString() wich will return a string.

So, to respond to your direct question (Can I add to redux an array of Conversation instances?)

YES YOU CAN.

Related