UNPKG

zstoreq

Version:

zustand-query is a library that helps you to query You can cal your api and get the data and store it in zustand store

90 lines (75 loc) โ€ข 3.43 kB
โšก Performance & Feature Comparison | Feature | ZStoreQ | react-query | | ------------------------- | ----------------------------------- | --------------------------------------- | | ๐Ÿ”„ State management | Zustand (global, reactive store) | Internal React context + caching | | ๐Ÿง  Data cache | In-memory with manual invalidation | In-memory, automated, smart GC | | ๐Ÿ•’ Stale time support | โœ… (manual staleTime support) | โœ… (automated + configurable) | | ๐Ÿ” Retries on failure | โœ… (manual retry if needed) | โœ… (configurable, exponential backoff) | | โฑ Background refresh | โŒ (can be added manually) | โœ… | | ๐Ÿš€ Performance overhead | ๐ŸŸข Lightweight (Zustand + fetch) | ๐ŸŸก Slightly heavier with built-in logic | | ๐Ÿช React 19 use() support | โœ… Native use of Suspense | โŒ Not yet optimized for React 19 | | ๐Ÿงฑ Custom store extension | โœ… (add persistence, logging, etc.) | โŒ Internal state not extensible | | ๐Ÿ”ฅ Global error handling | โœ… With setGlobalErrorHandler | โœ… via QueryClient config | | ๐ŸŒ DevTools | Optional via Zustand DevTools | โœ… Rich DevTools (out of the box) | | ๐Ÿ“ฆ Bundle size | ๐ŸŸข Tiny (~2KB + Zustand) | ๐ŸŸก ~13KB min+gzip | | ๐Ÿงฉ Mutation handling | โœ… useMutationZustand | โœ… Built-in mutations | | ๐Ÿ’พ Persistence support | โœ… Zustand has persist middleware | โœ… Needs manual setup | | ๐Ÿคน Custom control | ๐ŸŸข Full control over everything | โš ๏ธ Limited to API surface | | ๐Ÿ“‰ Learning curve | ๐ŸŸข Minimal if you know Zustand | ๐ŸŸก Slightly higher | ## Usage Example ### useQueryZustand ```typescript import React from "react"; import { useQueryZustand } from "zstoreq"; function fetchUser() { return fetch("/api/user").then((res) => res.json()); } function UserComponent() { const { data, error, isLoading, refetch } = useQueryZustand( "user", fetchUser, { staleTime: 5000, retry: 2, onError: (err) => console.error("Failed to fetch user:", err), } ); if (isLoading) return <div>Loading...</div>; if (error) return <div>Error: {error.message}</div>; return ( <div> <h1>{data.name}</h1> <button onClick={refetch}>Refetch</button> </div> ); } ``` ### useMutationZustand ```typescript import React from "react"; import { useMutationZustand } from "zstoreq"; function updateUser(data) { return fetch("/api/user", { method: "POST", body: JSON.stringify(data), }).then((res) => res.json()); } function UpdateUserComponent() { const { mutate, isLoading, error } = useMutationZustand({ mutationFn: updateUser, onSuccess: (data) => console.log("User updated:", data), onError: (err) => console.error("Failed to update user:", err), }); const handleSubmit = () => { mutate({ name: "New Name" }); }; return ( <div> <button onClick={handleSubmit} disabled={isLoading}> {isLoading ? "Updating..." : "Update User"} </button> {error && <div>Error: {error.message}</div>} </div> ); } ```