-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathCoreProvider.tsx
More file actions
33 lines (27 loc) · 943 Bytes
/
Copy pathCoreProvider.tsx
File metadata and controls
33 lines (27 loc) · 943 Bytes
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
30
31
32
33
import type { CoreStore, Stack } from "@stackflow/core";
import { createContext } from "react";
import { useDeferredValue, useSyncExternalStore } from "../shims";
export const CoreActionsContext = createContext<CoreStore["actions"]>(
null as any,
);
export const CoreStateContext = createContext<Stack>(null as any);
export interface CoreProviderProps {
coreStore: CoreStore;
children: React.ReactNode;
}
export const CoreProvider = ({ coreStore, children }: CoreProviderProps) => {
const stack = useSyncExternalStore(
coreStore.subscribe,
coreStore.actions.getStack,
coreStore.actions.getStack,
);
const deferredStack = useDeferredValue(stack);
return (
<CoreStateContext.Provider value={deferredStack}>
<CoreActionsContext.Provider value={coreStore.actions}>
{children}
</CoreActionsContext.Provider>
</CoreStateContext.Provider>
);
};
CoreProvider.displayName = "CoreProvider";