Skip to content

Persist Multiple Stores? #36

Description

@odesey

I am attempting to utilize your package to persist multiple mobx stores as per the example here and have my stores setup in a similar fashion to this:

/*   RootStore.js  */
import {UserStore, TodoStore } from './index.js'
import { AsyncTrunk } from 'mobx-sync';
import AsyncStorage from '@react-native-community/async-storage';

class RootStore {
    constructor() {
        this.userStore = new UserStore()
        this.todoStore = new TodoStore()
    }
}

const rootStore = new RootStore();

const trunk = new AsyncTrunk(rootStore, {
  storageKey: 'myAppStore',
  storage: AsyncStorage,
  onError: error => console.error('TRUNK ERROR', error)
});

export { rootStore, RootStore, trunk };

/*  UserStore.js  */
import {rootStore} from './RootStore.js'

class UserStore {
    constructor(rootStore) {
        this.rootStore = rootStore
    }

    getTodos(user) {
        // access todoStore through the root store
        return this.rootStore.todoStore.todos.filter((todo) => todo.author === user)
    }
}

/*  TodoStore.js */
import {rootStore} from './RootStore.js'

class TodoStore {
    @observable todos = []

    constructor(rootStore) {
        this.rootStore = rootStore
    }
}

I then call the following function to hydrate the stores on app start:

const initApp = () => {
  trunk.init().then(() => {
    console.log('hydrate done', rootStore.userStore); //always undefined
  }
}

rootStore.userStore is always undefined, how can I persist multiple stores using this package?

Thanks.

mobx: 5.15.4
react-native: 0.61.5
mobx-sync: 3.0.0
@react-native-community/async-storage: 1.11.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions