kurtgn
kurtgn

Reputation: 8710

redux and immutable.js: putting non-serializable objects into state

The official redux page highly recommends that only serializable objects be put into the state tree:

It is highly recommended that you only put plain serializable objects, arrays, and primitives into your store. It's technically possible to insert non-serializable items into the store, but doing so can break the ability to persist and rehydrate the contents of a store, as well as interfere with time-travel debugging.

However, on the same page, several sections later, there is a section about using immutable.js:

Immutable-focused libraries such as Immutable.JS have been designed to overcome the issues with immutability inherent within JavaScript, providing all the benefits of immutability with the performance your app requires.

Aren't these two ideas contradicting each other?

Upvotes: 3

Views: 1477

Answers (2)

Dima Vishnyakov
Dima Vishnyakov

Reputation: 1542

Time travel debugging (aka Redux devtools) supports Immutable.js. It is serializable.

Upvotes: 1

Josep
Josep

Reputation: 13071

No, they are not.

ImmutableJs instances are serializable. For instance, using transit-immutable.

Just because ImmutableJs instances are not plain javascript objects that does not mean that they are not serializable. Also, just using plain javascript objects doesn't totally guarantee that the state of your store is serializable. For instance, imagine a reducer doing something like this:

const sillyReducer = (state, action) => {
  if (action.type !== 'DO_SOMETHING_SILLY') return state;
  const a = {};
  const b = {next: a};
  a.next = b;
  return a;
}

Cyclic dependencies are pretty difficult to serialize.

Upvotes: 2

Related Questions