feat: use deferred store

This commit is contained in:
TrèsAbhi committed 2024-08-19 20:40:50 -05:00
1 parent 2727b2babe
commit 59324f7438
6 files changed
+30 -11

No files matched your search

+23 -2
View File
@@ -1,5 +1,12 @@
import { Draft, produce } from 'immer';
import { createContext, ReactNode, useContext, useRef } from 'react';
import {
createContext,
ReactNode,
useContext,
useEffect,
useRef,
useState,
} from 'react';
import { StoreApi, useStore as useStoreZustand } from 'zustand';
type StoreProviderProps<InitData> = {
@@ -49,6 +56,20 @@ export function createNextSafeStore<Store extends StoreApi<unknown>, InitData>(
);
}
function useDeferred<Slice = Type>(
initial: Slice,
selector: (store: Type) => Slice = (state) => state as Slice,
) {
const [state, setState] = useState(initial);
const trueState = use(selector);
useEffect(() => {
setState(trueState);
}, [trueState]);
return state;
}
function useMutation() {
const counterStoreContext = useStore();
@@ -61,5 +82,5 @@ export function createNextSafeStore<Store extends StoreApi<unknown>, InitData>(
};
}
return { Provider, use, useMutation, useStore };
return { Provider, use, useMutation, useStore, useDeferred };
}