Comments (5)
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.
that should only return something after you call registerRootStore. where are you calling it?
from mobx-keystone.
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.
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.
i had changed onAttachedToRootStore() wrongly to onInit() at some point. Thank you very much!
from mobx-keystone.
Related Issues (20)
- How to provide default/fixed value for inheriting class? HOT 4
- Mental model and idea behind keystone's tweaks/patches related to mobx observe
- Root Registry / Backrefs with deep observation HOT 5
- Keystone types supporting `onActionMiddleware` HOT 3
- Is it valid to start actions in onActionMiddleware's onStart/onFinish? HOT 2
- How to listen "initial" onPatches on RootStore?
- incorrect detaches from root store when applying snapshot HOT 4
- model does not detached from root store if it in array HOT 4
- Exception when detaching model that have active reaction with reference HOT 4
- Not able to access static methods of parent class from child class when inheriting with generic types. HOT 1
- Does it support Typescript 5.X features? HOT 3
- Unnecessary loops causes low performance, how to optimize the performance HOT 2
- Binary data in models HOT 3
- yjs Map not recommended for key-value data. HOT 4
- Thank you HOT 2
- WeakMap key must be an Object HOT 5
- Getting 'extendObservable' only accepts plain objects as second argument HOT 1
- onInit does not execute from static creation method HOT 3
- No static checks to prevent assignment outside of an action HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mobx-keystone.