Giter Site home page Giter Site logo

Comments (5)

xaviergonz avatar xaviergonz commented on June 8, 2024 1

That's expected (and the behavior hasn't changed from the first versions):

// here no rootstore is registered

rootStore = new RootStore({
  ticketList: new TicketList({}), // here onInit is called, where no rootstore is still registered
});

// here is where the root store gets actually registered
registerRootStore(rootStore);

if you want to use the rootstore as part of a "sort of" initialization phase use onAttachedToRootStore instead

https://mobx-keystone.js.org/root-stores#onattachedtorootstore

that callback gets called whenever an object "enters/leaves" a rootstore, therefore in your case TicketList.onAttachedToRootStore will get called at the very same time that you call registerRootstore

from mobx-keystone.

xaviergonz avatar xaviergonz commented on June 8, 2024

that should only return something after you call registerRootStore. where are you calling it?

from mobx-keystone.

AshwinDeTaeye avatar AshwinDeTaeye commented on June 8, 2024

I call registerRootStore(rootStore); in my createRootStore function:


export async function createRootStore() {
  const rawStored = localStorage.getItem('newst');
  if (typeof rawStored === 'string') {
    try {
      const savedRootStore = await JSON.parse(rawStored);
      rootStore = fromSnapshot<RootStore>(savedRootStore);
      registerRootStore(rootStore);
    } catch (e) {
      console.log('There was an error restoring the initial localStorage');
    }
  } else {
    try {
      rootStore = new RootStore({
        ticketList: new TicketList({}),
        basket: new Basket({}),
      });
    } catch (e) {
      console.log('There was an error creating rootStore from scratch', e);
    }
    registerRootStore(rootStore);
  }

  return rootStore;
}

I call this function at the start of the app:

(async () => {
  const st = await createRootStore();
  return console.log('1. CREATED rootStore');
})();

from mobx-keystone.

AshwinDeTaeye avatar AshwinDeTaeye commented on June 8, 2024

If i remove all the registerRootStore() from my code, i get the same result the app starts and works. On the home page, there is already some data from mobx keystone state. But when i do getRootStore(this) in another part of the tree, it doesn't give me the rootStore.

As a test, i have placed another registerRootStore() after where i create the rootStore. And then i get the error: object already registered as root store

So it looks like it has been registered successfully in the createRootStore function.

(async () => {
  const st = await createRootStore();
  registerRootStore(st);
  return console.log('1. CREATED rootStore');
})();

Above gives the error: object already registered as root store

from mobx-keystone.

AshwinDeTaeye avatar AshwinDeTaeye commented on June 8, 2024

i had changed onAttachedToRootStore() wrongly to onInit() at some point. Thank you very much!

from mobx-keystone.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.