diff --git a/packages/ui/src/index.tsx b/packages/ui/src/index.tsx index 2b130c3..bc77b0c 100644 --- a/packages/ui/src/index.tsx +++ b/packages/ui/src/index.tsx @@ -16,7 +16,6 @@ export type MatchState = _MatchState & { export type Selector = (state: MatchState) => T; -// TODO Type this properly export type Store = StoreApi>; export const storeContext = createContext(null); @@ -209,3 +208,22 @@ export const useUsernames = (): Record => { export const useMyUserId = () => { return useSelector((state) => state.userId); }; + +/* Return the store object. + * + * This is useful to access the state without subscribing to changes: + * ``` + * const store = useStore() + * const x = store.getState().board.x + * ``` + * */ +export function useStore() { + const store = useContext(storeContext) as Store; + if (store === null) { + throw new Error("Store is `null`, did you forget ?"); + } + return store; +} + +/* Convenience function to get a typed `useStore` hook. */ +export const makeUseStore = () => useStore;