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
Markdown
โก 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>
);
}
```