lostpebble / pullstate

Simple state stores using immer and React hooks - re-use parts of your state by pulling it anywhere you like!

Home Page:https://lostpebble.github.io/pullstate

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug] Update does not work with value from yup

nahtnam opened this issue · comments

Hello,

I found an interesting bug:

If you set up a yup model and try to setup a new store based on it like this:

let test;
try {
  test = schema.validateSync(schema.cast(value), { stripUnknown: true });
} catch {
  test = schema.cast({});
}
const Store = new Store(test);
Store.subscribe(
  (s) => s,
  (newValue) => {
    console.log('here');
  },
);

If you try this out and update the state, here will not print. However, if you add the following line:

test = JSON.parse(JSON.stringify(test))

Then it works fine. Maybe yup does something weird with the object it returns. Would be interesting to figure out why this doesn't work

Hi @nahtnam ,

I think I need a bit more context here. Where is datum coming from? The type of that data is probably quite important, or is that a mis-type and you meant test. And where are you updating your store to trigger the subscribe() function?

Also, subscribe will only trigger on a change, as detected by fast-deep-equal. If nothing changed according to that algorithm, then it won't trigger.

You should console.log the value returned from yup and see. It might be a certain type of object which doesn't compare very well from one to the next, unless you remove it from its "object" with serialization and parsing again, like you did.

Generally, Pullstate should be for serializable data. Specialized objects (basically, functions or classes in JavaScript world) are not really the target data type because of these kinds of issues (not easy to compare one to one).

I think I need a bit more context here. Where is datum coming from? The type of that data is probably quite important, or is that a mis-type and you meant test. And where are you updating your store to trigger the subscribe() function?

Sorry, I meant test. Left over from where I copied and pasted it.

I tried to reproduce this in a codesandbox however I couldn't. I think I just might be doing something wrong when loading and parsing from localstorage but I was able to fix it with JSON.stringify so no big deal.