-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.tsx
23 lines (19 loc) · 837 Bytes
/
store.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { createContext, useContext } from 'react';
import { Instance } from 'mobx-state-tree';
import { RootModel } from './RootModel';
import React = require('react');
export const rootInstance = RootModel.create({});
type RootInstance = Instance<typeof RootModel>;
const RootStoreContext = createContext<null | RootInstance>(rootInstance);
export const Provider = RootStoreContext.Provider;
export function useMst(): RootInstance {
const store = useContext(RootStoreContext);
if (store === null) {
throw new Error('Store cannot be null, please add a context provider');
}
return store as RootInstance;
}
export type RootType = Instance<typeof RootModel>;
export const ProviderWrapper = ({ children }: { children: any }): any => {
return <Provider value={rootInstance}>{children}</Provider>;
};