ConrabOpto / mst-query

Query library for mobx-state-tree
MIT License
116 stars 8 forks source link

Anyway to access the rootStore without using context / useRootstore hook #40

Closed geohuz closed 1 year ago

geohuz commented 1 year ago

In react-router v6 there is a data router which expose function action/loader calls fetch api, but these functions are out of component scope, I'm wondering how can I reference the rootStore from the normal function?

k-ode commented 1 year ago

Yes, you can initialize the query client manually.

export const queryClient = new QueryClient({ RootStore });
queryClient.init(initialData, env); // internally calls RootStore.create(initialData, env)
const rootStore = queryClient.rootStore;
geohuz commented 1 year ago

@k-ode thanks! That's fair enough.