floppy-disk 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -30
- package/esm/preact/create-query.js +1 -1
- package/esm/react/create-query.js +1 -1
- package/lib/preact/create-query.js +1 -1
- package/lib/react/create-query.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
A lightweight, simple, and powerful state management library.
|
|
4
4
|
|
|
5
|
-
This library was highly-inspired by [Zustand](https://www.npmjs.com/package/zustand) and [
|
|
5
|
+
This library was highly-inspired by [Zustand](https://www.npmjs.com/package/zustand) and [TanStack-Query](https://tanstack.com/query).
|
|
6
|
+
Both are awesome state manager. That's why this Floppy Disk library behaves like them, but with small DX improvement, **more power**, and **less bundle size**.
|
|
6
7
|
|
|
7
8
|
**Bundle Size Comparison:**
|
|
8
9
|
|
|
@@ -23,7 +24,7 @@ import { createQuery, createMutation } from 'floppy-disk'; // 8.2 kB (gzipped: 2
|
|
|
23
24
|
- Using Zustand & React-Query: https://demo-zustand-react-query.vercel.app/
|
|
24
25
|
👉 Total: **309.21 kB**
|
|
25
26
|
- Using Floppy Disk: https://demo-floppy-disk.vercel.app/
|
|
26
|
-
👉 Total: **270.
|
|
27
|
+
👉 Total: **270.87 kB** 🎉
|
|
27
28
|
|
|
28
29
|
## Key Features
|
|
29
30
|
|
|
@@ -50,6 +51,8 @@ import { createQuery, createMutation } from 'floppy-disk'; // 8.2 kB (gzipped: 2
|
|
|
50
51
|
- Can be used with literally any asynchronous data fetching client, including GraphQL ✅
|
|
51
52
|
- Create mutation ✅
|
|
52
53
|
|
|
54
|
+
**View official documentation on [floppy-disk.vercel.app](https://floppy-disk.vercel.app/)**
|
|
55
|
+
|
|
53
56
|
## Table of Contents
|
|
54
57
|
|
|
55
58
|
- [Key Features](#key-features)
|
|
@@ -60,6 +63,7 @@ import { createQuery, createMutation } from 'floppy-disk'; // 8.2 kB (gzipped: 2
|
|
|
60
63
|
- [Stores](#stores)
|
|
61
64
|
- [Query \& Mutation](#query--mutation)
|
|
62
65
|
- [Query State \& Network Fetching State](#query-state--network-fetching-state)
|
|
66
|
+
- [Inherited from createStores](#inherited-from-createstores)
|
|
63
67
|
- [Single Query](#single-query)
|
|
64
68
|
- [Single Query with Params](#single-query-with-params)
|
|
65
69
|
- [Paginated Query or Infinite Query](#paginated-query-or-infinite-query)
|
|
@@ -431,6 +435,35 @@ Here is the flow of the query data state:
|
|
|
431
435
|
For network fetching state, we use `isWaiting`.
|
|
432
436
|
The value will be `true` if the query is called and still waiting for the response.
|
|
433
437
|
|
|
438
|
+
### Inherited from createStores
|
|
439
|
+
|
|
440
|
+
The `createQuery` function inherits functionality from the `createStores` function, allowing us to perform the same result and actions available in `createStores`.
|
|
441
|
+
|
|
442
|
+
```tsx
|
|
443
|
+
const useMyQuery = createQuery(myQueryFn, {
|
|
444
|
+
// 👇 Same as createStores options
|
|
445
|
+
defaultDeps: undefined,
|
|
446
|
+
onFirstSubscribe: (state) => console.log('onFirstSubscribe', state),
|
|
447
|
+
onSubscribe: (state) => console.log('onSubscribe', state),
|
|
448
|
+
onUnsubscribe: (state) => console.log('onUnsubscribe', state),
|
|
449
|
+
onLastUnsubscribe: (state) => console.log('onLastUnsubscribe', state),
|
|
450
|
+
onBeforeChangeKey: (nextKey, prevKey) => console.log('Store key changed', nextKey, prevKey),
|
|
451
|
+
|
|
452
|
+
// ... other createQuery options
|
|
453
|
+
});
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
Custom reactivity (dependency array) also works:
|
|
457
|
+
|
|
458
|
+
```tsx
|
|
459
|
+
function QueryLoader() {
|
|
460
|
+
// This component doesn't care whether the query is success or error.
|
|
461
|
+
// It just listening to network fetching state. 👇
|
|
462
|
+
const { isWaiting } = useMyQuery((state) => [state.isWaiting]);
|
|
463
|
+
return <div>Is network fetching? {String(isWaiting)}</div>;
|
|
464
|
+
}
|
|
465
|
+
```
|
|
466
|
+
|
|
434
467
|
### Single Query
|
|
435
468
|
|
|
436
469
|
```jsx
|
|
@@ -458,22 +491,18 @@ function SingleQuery() {
|
|
|
458
491
|
|
|
459
492
|
> Example: [https://codesandbox.io/.../examples/react/query](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/query)
|
|
460
493
|
|
|
461
|
-
Custom reactivity:
|
|
462
|
-
|
|
463
|
-
```jsx
|
|
464
|
-
// This component doesn't care whether the query is success or error.
|
|
465
|
-
// It just listening to network fetching state. 👇
|
|
466
|
-
function SingleQueryLoader() {
|
|
467
|
-
const { isWaiting } = useGitHubQuery((state) => [state.isWaiting]);
|
|
468
|
-
return <div>Is network fetching? {String(isWaiting)}</div>;
|
|
469
|
-
}
|
|
470
|
-
```
|
|
471
|
-
|
|
472
494
|
Actions:
|
|
473
495
|
|
|
496
|
+
Normally, we don't need reactivity for the actions.
|
|
497
|
+
Therefore, using `get` method will be better, since it will not re-render the component when a query state changed.
|
|
498
|
+
|
|
474
499
|
```jsx
|
|
475
500
|
function Actions() {
|
|
476
|
-
const { fetch, forceFetch, reset } = useGitHubQuery(
|
|
501
|
+
const { fetch, forceFetch, reset } = useGitHubQuery.get();
|
|
502
|
+
|
|
503
|
+
// Or like this:
|
|
504
|
+
// const { isLoading, data, error, fetch, forceFetch, reset } = useGitHubQuery();
|
|
505
|
+
|
|
477
506
|
return (
|
|
478
507
|
<>
|
|
479
508
|
<button onClick={fetch}>Call query if the query data is stale</button>
|
|
@@ -524,22 +553,14 @@ function MyComponent() {
|
|
|
524
553
|
Get data or do something outside component:
|
|
525
554
|
|
|
526
555
|
```jsx
|
|
527
|
-
const getData = () =>
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
function Actions() {
|
|
536
|
-
return (
|
|
537
|
-
<>
|
|
538
|
-
<button onClick={getData}>Get Data</button>
|
|
539
|
-
<button onClick={resetQuery}>Reset query</button>
|
|
540
|
-
</>
|
|
541
|
-
);
|
|
542
|
-
}
|
|
556
|
+
const getData = () => console.log(useGitHubQuery.get().data);
|
|
557
|
+
const resetQuery = () => useGitHubQuery.get().reset();
|
|
558
|
+
|
|
559
|
+
// Works just like createStores
|
|
560
|
+
useMyQuery.get(/* ... */);
|
|
561
|
+
useMyQuery.set(/* ... */);
|
|
562
|
+
useMyQuery.subscribe(/* ... */);
|
|
563
|
+
useMyQuery.getSubscribers(/* ... */);
|
|
543
564
|
```
|
|
544
565
|
|
|
545
566
|
### Single Query with Params
|
|
@@ -273,7 +273,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
273
273
|
useQuery.setInitialResponse = ({ key, response }) => {
|
|
274
274
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
275
275
|
useState(() => {
|
|
276
|
-
if (response === undefined)
|
|
276
|
+
if (response === undefined || useQuery.get(key).data)
|
|
277
277
|
return;
|
|
278
278
|
const newPageParam = getNextPageParam(response, 1);
|
|
279
279
|
useQuery.set(key, {
|
|
@@ -273,7 +273,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
273
273
|
useQuery.setInitialResponse = ({ key, response }) => {
|
|
274
274
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
275
275
|
useState(() => {
|
|
276
|
-
if (response === undefined)
|
|
276
|
+
if (response === undefined || useQuery.get(key).data)
|
|
277
277
|
return;
|
|
278
278
|
const newPageParam = getNextPageParam(response, 1);
|
|
279
279
|
useQuery.set(key, {
|
|
@@ -276,7 +276,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
276
276
|
useQuery.setInitialResponse = ({ key, response }) => {
|
|
277
277
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
278
278
|
(0, hooks_1.useState)(() => {
|
|
279
|
-
if (response === undefined)
|
|
279
|
+
if (response === undefined || useQuery.get(key).data)
|
|
280
280
|
return;
|
|
281
281
|
const newPageParam = getNextPageParam(response, 1);
|
|
282
282
|
useQuery.set(key, {
|
|
@@ -276,7 +276,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
276
276
|
useQuery.setInitialResponse = ({ key, response }) => {
|
|
277
277
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
278
278
|
(0, react_1.useState)(() => {
|
|
279
|
-
if (response === undefined)
|
|
279
|
+
if (response === undefined || useQuery.get(key).data)
|
|
280
280
|
return;
|
|
281
281
|
const newPageParam = getNextPageParam(response, 1);
|
|
282
282
|
useQuery.set(key, {
|