aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/RootStoreProvider.tsx
blob: 70ac77765f3d21554c19ff7bd5451266b2147744 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import React, { type ReactNode, createContext, useContext } from 'react';

import type RootStore from './RootStore';

const StoreContext = createContext<RootStore | undefined>(undefined);

export function useRootStore(): RootStore {
  const rootStore = useContext(StoreContext);
  if (!rootStore) {
    throw new Error('useRootStore must be used within RootStoreProvider');
  }
  return rootStore;
}

export default function RootStoreProvider({
  children,
  rootStore,
}: {
  children?: ReactNode;
  rootStore: RootStore;
}): JSX.Element {
  return (
    <StoreContext.Provider value={rootStore}>{children}</StoreContext.Provider>
  );
}

RootStoreProvider.defaultProps = {
  children: undefined,
};