Skip to content

Commit

Permalink
fix useRef usage in tests (#7)
Browse files Browse the repository at this point in the history
dai-shi authored Apr 22, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent dedfeea commit 9770652
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions tests/03_component.spec.tsx
Original file line number Diff line number Diff line change
@@ -16,10 +16,15 @@ type ExtractState<S> = S extends {
function createZustandContext<Store extends StoreApi<unknown>>(
initializeStore: () => Store,
) {
const Context = createContext<Store | null>(null);
const Context = createContext<Store | undefined>(undefined);
const StoreProvider = ({ children }: { children: ReactNode }) => {
const store = useRef(initializeStore()).current;
return <Context.Provider value={store}>{children}</Context.Provider>;
const storeRef = useRef<Store>();
if (!storeRef.current) {
storeRef.current = initializeStore();
}
return (
<Context.Provider value={storeRef.current}>{children}</Context.Provider>
);
};
function useStoreApi() {
const store = useContext(Context);

0 comments on commit 9770652

Please sign in to comment.