ctrlplusb / easy-peasy

Vegetarian friendly state for React

Home Page:https://easy-peasy.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Persist state with boolean false as default value?

call0fcode opened this issue · comments

I'm facing the issue that a state with a boolean false as default value is not being persisted after page refresh and the corresponding state rehydrating.

This would be an example of the behavior:

I have this model and it is persisted...

const sampleModel = {
  sampleKey1: null,
  setSampleKey1: action((state, payload) => {
    state.sampleKey1 = payload;
  }),
  sampleKey2: false,
  setSampleKey2: action((state, payload) => {
    state.sampleKey2 = payload;
  }),
};

If I set both states to something different using the corresponding actions and then refresh the page (change the location or similar), then the only state being persisted is the sampleKey1. For sampleKey2 it's somehow reseted to false although I'm pretty sure I haven't updated it before refreshing the page, changing de location, etc.

Is this the expected behavior and should false default states remain as false on rehydrating?

I'm still on the v5.1.0 because I faced problems with persistency on v5.2.0.

Ok, I realized I was changing the data type from boolean to string and confirm that, due to how the persist merge strategies work, the store model value has been used in the resulting merged persisting state.

From the mergeDeep strategy doc:

It will also verify the types of data at each key. If there is a misalignment (ignoring null or undefined) then it will opt for using the data from the store model instead as this generally indicates that the respective state has been refactored.

Now I understand why my null default values are being replaced on the result.