What is best practice for fetching data in Zustand? #1415
-
Hi, I'm new to Zustand. Is there more suitable way than initialize store in useEffect? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 23 replies
-
Async actions https://github.com/pmndrs/zustand#async-actions should be best practice? |
Beta Was this translation helpful? Give feedback.
-
@vaynevayne its not OK pointing to chinese docs ! |
Beta Was this translation helpful? Give feedback.
-
Hi all, I am also trying to find good resources about the same subject. I'm also new to Zustand and the best practices that one should follow for global state management. I was thinking of using SWR in addition to Zustand's persist middleware for getting the most out of it. What are some of the widely followed patterns for combining Zustand, persist and SWR? |
Beta Was this translation helpful? Give feedback.
-
const createDataStore = () =>
createStore<TypeStore>()(
immer(
persist(
(set) => ({
data: [],
index: 0,
saved: {},
setIndex: (index: number) => set({ index }),
seData: (data: TypeData[]) => set({ data }),
setSaved: (value: string) =>
set((state) => {
state.saved[state.data[state.index]] = value;
}),
}),
{
name: "data",
}
)
)
);
"use client";
import createDataStore from "./store";
const DataStoreContext = createContext<ReturnType<typeof createDataStore> | null>(null);
const DataStoreProvider = ({ children }: { children: ReactNode }) => {
const storeRef = useRef<ReturnType<typeof createDataStore>>();
if (!storeRef.current) {
storeRef.current = createDataStore();
}
return (
<DataStoreContext.Provider value={storeRef.current}>{children}</DataStoreContext.Provider>
);
};
const useDataStore = <T,>(selector: (store: IDataStore) => T): T => {
const dataStoreContext = useContext(DataStoreContext);
if (!dataStoreContext) {
throw new Error(`useDataStore must be use within DataStoreProvider`);
}
//// *useStoreWithEqualityFn for preventing re-rendring* ////
return useStoreWithEqualityFn(dataStoreContext, selector);
};
import DataStoreProvider from "./provider";
export default function RootLayout({
children,
}: Readonly<{
children: ReactNode;
}>) {
return <DataStoreProvider>{children}</DataStoreProvider>;
}
"use client";
import useDataStore from "./provider";
export default function Home() {
const setData = useDataStore((state) => state.setData);
const { isLoading } = useQuery({
queryKey: ["data"],
queryFn: async () => {
const response = await fetch("/data.json");
return await response.json();
},
onSuccess(data) {
setData(data);
},
});
return <main>{isLoading ? <Supense /> : <Comp />}</main>;
} |
Beta Was this translation helpful? Give feedback.
-
@dai-shi My current implementation: Routes:
userstore
I don't see network request in |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
It's useful for me. const Com = () => {
if (!state) {
throw stateSetter();
}
return xxx;
}; <ErrorBoundary FallbackComponent={...}>
<React.Suspense fallback={...}>
<Com />
</React.Suspense>
</ErrorBoundary> |
Beta Was this translation helpful? Give feedback.
Async actions https://github.com/pmndrs/zustand#async-actions should be best practice?