-
Notifications
You must be signed in to change notification settings - Fork 10
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
Root store pattern #4
Comments
Hi, thanks for the question, I plan to add the root store pattern sometime this week. |
looking forward for rootstore pattern as well. I think the above code should work, but iam interested in seeing what @borekb 's implementation would be. Thanks for the repo btw. |
First of all, thank you for your repo, its helping me where lots of others cannot. Did you end up getting time to implement the rootStore? I am looking at your repo and it doesnt look like you did it? |
Sorry folks, this has not been high on my priority list 😄. Here's a copy/paste from our /**
* Implements the RootStore pattern and a universal way to work with all the stores at once.
* For example, `_app.tsx` does not iterate over stores to get their data, it calls `rootStore.toJSON()`.
*/
export class RootStore implements MobxStore {
brandStore: BrandStore;
cartStore: CartStore;
categoryStore: CategoryStore;
constructor(initialData: RootStoreInitialData | null, apolloClient: ApolloClient<{}>) {
this.brandStore = getStore(BrandStore, initialData ? initialData.brandStoreInitialData : null, apolloClient, this);
this.cartStore = getStore(CartStore, initialData ? initialData.cartStoreInitialData : null, apolloClient, this);
this.categoryStore = getStore(
CategoryStore,
initialData ? initialData.categoryStoreInitialData : null,
apolloClient,
this
);
}
async fetchInitialDataDuringSSR(ctx: NextPageContext): Promise<void> {
await Promise.all(
this.stores.map((store: MobxStore) => {
if (store.fetchInitialDataDuringSSR) {
return store.fetchInitialDataDuringSSR(ctx);
} else {
return Promise.resolve();
}
})
);
}
toJSON(): RootStoreInitialData {
return {
brandStoreInitialData: this.brandStore.toJSON(),
cartStoreInitialData: this.cartStore.toJSON(),
categoryStoreInitialData: this.categoryStore.toJSON(),
};
}
private get stores() {
return [this.cartStore, this.categoryStore];
}
} |
Hi!
Thank you for this implementation, but I don’t understand a bit. Could you provide an example how to use one store inside another (aka. RootStore pattern) using "initial data"?
It's correct use something like this?
Then
The text was updated successfully, but these errors were encountered: