Skip to content
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

Initial State isn't merged -- undefined is not assignable to type #25

Closed
mxmtsk opened this issue Jan 24, 2020 · 3 comments
Closed

Initial State isn't merged -- undefined is not assignable to type #25

mxmtsk opened this issue Jan 24, 2020 · 3 comments
Labels
solution: duplicate This issue or pull request already exists solution: intended behavior This is not a bug and is expected behavior

Comments

@mxmtsk
Copy link

mxmtsk commented Jan 24, 2020

I have the following store setup:

export const createStore = (): RootStoreModel => {
  const app = AppModel.create({});
  const auth = AuthModel.create({});
  const organizations = OrganizationsStore.create({});

  const rootStoreInstance = RootStore.create(
    {
      app,
      auth,
      organizations,
    },
  );

  persist('persistedRootStore', rootStoreInstance, {
    storage: AsyncStorage,
    jsonify: true,
    whitelist: ['auth', 'organizations'],
  }).then(() => {
    console.log('💦', 'App hydrated');
    rootStoreInstance.app.setHydrated(true);
  });

  return rootStoreInstance;
};

This throws the following error on hydration:

Error: [mobx-state-tree] Error while converting {"auth":{"user":null,"token":null},"organizations":{"list":[]}} to RootStore:

at path "/app" value `undefined` is not assignable to type: `AppModel` (Value is not a plain object).

Error: [mobx-state-tree] Error while converting {"auth":{"user":null,"token":null},"organizations":{"list":[]}} to RootStore:

When I remove the whitelist so that the whole store is persisted it works fine. Am I doing anything wrong? I'm on mobx-state-tree@^3.15.0. and mst-persist@^0.1.3.

@agilgur5
Copy link
Owner

agilgur5 commented Jan 24, 2020

Hi @mxmtsk ! Apologies for the brief response, I'm out of the country till next week.

So when whitelist is not set, every key will be persisted. When it is set, only the keys listed will be persisted. In this case, app is not in the whitelist, so it's hydrated as undefined. But your RootStoreModel is typed such that app must exist (not a types.maybe or types.optional), so this will error.

So this isn't an mst-persist bug. But you might be getting confused by the fact that app is undefined instead of the initial state set during create. This is because mst-persist currently does not merge keys during hydration, it does a hard set, overwriting the initial state. There's an open issue for configurable merge strategies at #5 . Please up-vote that if that's what you're looking for.

@mxmtsk
Copy link
Author

mxmtsk commented Jan 24, 2020

@agilgur5 Thanks for the quick reply. I found #5 aswell and thought it might be related. Your explanation makes me understand it more though and I found a way to workaround this - thank you!

@agilgur5 agilgur5 changed the title undefined is not assignable to type Initial State isn't merged -- undefined is not assignable to type Feb 3, 2020
@agilgur5 agilgur5 added the solution: duplicate This issue or pull request already exists label Feb 27, 2020
@agilgur5 agilgur5 added the solution: intended behavior This is not a bug and is expected behavior label Oct 25, 2020
@Tymix919

This comment was marked as spam.

Repository owner locked as resolved and limited conversation to collaborators Jul 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
solution: duplicate This issue or pull request already exists solution: intended behavior This is not a bug and is expected behavior
Projects
None yet
Development

No branches or pull requests

3 participants