diff options
Diffstat (limited to 'subprojects/frontend/src/RootStoreProvider.tsx')
-rw-r--r-- | subprojects/frontend/src/RootStoreProvider.tsx | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/subprojects/frontend/src/RootStoreProvider.tsx b/subprojects/frontend/src/RootStoreProvider.tsx new file mode 100644 index 00000000..70ac7776 --- /dev/null +++ b/subprojects/frontend/src/RootStoreProvider.tsx | |||
@@ -0,0 +1,29 @@ | |||
1 | import React, { type ReactNode, createContext, useContext } from 'react'; | ||
2 | |||
3 | import type RootStore from './RootStore'; | ||
4 | |||
5 | const StoreContext = createContext<RootStore | undefined>(undefined); | ||
6 | |||
7 | export function useRootStore(): RootStore { | ||
8 | const rootStore = useContext(StoreContext); | ||
9 | if (!rootStore) { | ||
10 | throw new Error('useRootStore must be used within RootStoreProvider'); | ||
11 | } | ||
12 | return rootStore; | ||
13 | } | ||
14 | |||
15 | export default function RootStoreProvider({ | ||
16 | children, | ||
17 | rootStore, | ||
18 | }: { | ||
19 | children?: ReactNode; | ||
20 | rootStore: RootStore; | ||
21 | }): JSX.Element { | ||
22 | return ( | ||
23 | <StoreContext.Provider value={rootStore}>{children}</StoreContext.Provider> | ||
24 | ); | ||
25 | } | ||
26 | |||
27 | RootStoreProvider.defaultProps = { | ||
28 | children: undefined, | ||
29 | }; | ||