floppy-disk 2.0.0-beta.3 → 2.0.0
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 +81 -9
- package/package.json +13 -12
package/README.md
CHANGED
|
@@ -17,16 +17,43 @@ import {
|
|
|
17
17
|
useInfiniteQuery,
|
|
18
18
|
useMutation,
|
|
19
19
|
} from '@tanstack/react-query'; // 41 kB (gzipped: 11 kB)
|
|
20
|
-
import { createQuery, createMutation } from 'floppy-disk'; //
|
|
20
|
+
import { createQuery, createMutation } from 'floppy-disk'; // 8.1 kB (gzipped: 2.7 kB) 🎉
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
- Using Zustand & React-Query: https://demo-zustand-react-query.vercel.app/
|
|
24
|
-
👉 Total: **309.
|
|
24
|
+
👉 Total: **309.21 kB**
|
|
25
25
|
- Using Floppy Disk: https://demo-floppy-disk.vercel.app/
|
|
26
|
-
👉 Total: **
|
|
26
|
+
👉 Total: **284.8 kB** 🎉
|
|
27
|
+
|
|
28
|
+
## Key Features
|
|
29
|
+
|
|
30
|
+
- Create store ✅
|
|
31
|
+
- Yes, it also support middleware & set/get state outside component ✅
|
|
32
|
+
- Store event (`onSubscribe`, `onUnsubscribe`, etc.) ✅
|
|
33
|
+
- Use store as local state manager ✅
|
|
34
|
+
- Create stores (yes, it's plural) ✅ \*_controlled with a store key_
|
|
35
|
+
- Create query ✅
|
|
36
|
+
- Dedupe multiple request ✅
|
|
37
|
+
- Auto-update stale data (stale-while-revalidate) ✅
|
|
38
|
+
- Enable/disable query ✅
|
|
39
|
+
- Auto-fetch or manual (lazy query) ✅
|
|
40
|
+
- Retry on error (retry count, retry delay) ✅
|
|
41
|
+
- SSR/SSG's initial query data ✅
|
|
42
|
+
- Optimistic update ✅
|
|
43
|
+
- Invalidate query ✅
|
|
44
|
+
- Reset query ✅
|
|
45
|
+
- Query with param (query key) ✅
|
|
46
|
+
- Infinite query ✅
|
|
47
|
+
- Get query data outside component ✅
|
|
48
|
+
- Custom reactivity ✅
|
|
49
|
+
- Fetching mechanisms are agnostically built on promises ✅
|
|
50
|
+
- Can be used with literally any asynchronous data fetching client, including GraphQL ✅
|
|
51
|
+
- Create mutation ✅
|
|
27
52
|
|
|
28
53
|
## Table of Contents
|
|
29
54
|
|
|
55
|
+
- [Key Features](#key-features)
|
|
56
|
+
- [Table of Contents](#table-of-contents)
|
|
30
57
|
- [Store](#store)
|
|
31
58
|
- [Basic Concept](#basic-concept)
|
|
32
59
|
- [Advanced Concept](#advanced-concept)
|
|
@@ -446,12 +473,11 @@ Actions:
|
|
|
446
473
|
|
|
447
474
|
```jsx
|
|
448
475
|
function Actions() {
|
|
449
|
-
const { fetch, forceFetch,
|
|
476
|
+
const { fetch, forceFetch, reset } = useGitHubQuery(() => []);
|
|
450
477
|
return (
|
|
451
478
|
<>
|
|
452
479
|
<button onClick={fetch}>Call query if the query data is stale</button>
|
|
453
480
|
<button onClick={forceFetch}>Call query</button>
|
|
454
|
-
<button onClick={markAsStale}>Mark as stale</button>
|
|
455
481
|
<button onClick={reset}>Reset query</button>
|
|
456
482
|
</>
|
|
457
483
|
);
|
|
@@ -470,7 +496,7 @@ const useGitHubQuery = createQuery(
|
|
|
470
496
|
{
|
|
471
497
|
fetchOnMount: false,
|
|
472
498
|
enabled: () => !!useUserQuery.get().data?.user,
|
|
473
|
-
select: (response) => response.name
|
|
499
|
+
select: (response) => response.name,
|
|
474
500
|
staleTime: Infinity, // Never stale
|
|
475
501
|
retry: 0, // No retry
|
|
476
502
|
onSuccess: (response) => {},
|
|
@@ -478,6 +504,21 @@ const useGitHubQuery = createQuery(
|
|
|
478
504
|
onSettled: () => {},
|
|
479
505
|
},
|
|
480
506
|
);
|
|
507
|
+
|
|
508
|
+
function MyComponent() {
|
|
509
|
+
const { data, response } = useGitHubQuery();
|
|
510
|
+
/**
|
|
511
|
+
* Since in option we select the data like this:
|
|
512
|
+
* select: (response) => response.name
|
|
513
|
+
*
|
|
514
|
+
* The return will be:
|
|
515
|
+
* {
|
|
516
|
+
* response: { id: 677863376, name: "floppy-disk", ... },
|
|
517
|
+
* data: "floppy-disk",
|
|
518
|
+
* ...
|
|
519
|
+
* }
|
|
520
|
+
*/
|
|
521
|
+
}
|
|
481
522
|
```
|
|
482
523
|
|
|
483
524
|
Get data or do something outside component:
|
|
@@ -620,15 +661,15 @@ function Login() {
|
|
|
620
661
|
<div>
|
|
621
662
|
<button
|
|
622
663
|
disabled={isWaiting}
|
|
623
|
-
onClick={() =>
|
|
664
|
+
onClick={() => {
|
|
624
665
|
mutate({ email: 'foo@bar.baz', password: 's3cREt' }).then(({ response, error }) => {
|
|
625
666
|
if (error) {
|
|
626
667
|
showToast('Login failed');
|
|
627
668
|
} else {
|
|
628
669
|
showToast('Login success');
|
|
629
670
|
}
|
|
630
|
-
})
|
|
631
|
-
}
|
|
671
|
+
});
|
|
672
|
+
}}
|
|
632
673
|
>
|
|
633
674
|
Login
|
|
634
675
|
</button>
|
|
@@ -637,6 +678,37 @@ function Login() {
|
|
|
637
678
|
}
|
|
638
679
|
```
|
|
639
680
|
|
|
681
|
+
Optimistic update:
|
|
682
|
+
|
|
683
|
+
```jsx
|
|
684
|
+
function SaveProduct() {
|
|
685
|
+
const { mutate, isWaiting } = useEditProductMutation();
|
|
686
|
+
const { getValues } = useFormContext();
|
|
687
|
+
|
|
688
|
+
return (
|
|
689
|
+
<button
|
|
690
|
+
disabled={isWaiting}
|
|
691
|
+
onClick={() => {
|
|
692
|
+
const payload = getValues();
|
|
693
|
+
|
|
694
|
+
const { revert, invalidate } = useProductQuery.optimisticUpdate({
|
|
695
|
+
key: { id: payload.id },
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
mutate(payload).then(({ response, error }) => {
|
|
699
|
+
if (error) {
|
|
700
|
+
revert();
|
|
701
|
+
}
|
|
702
|
+
invalidate();
|
|
703
|
+
});
|
|
704
|
+
}}
|
|
705
|
+
>
|
|
706
|
+
Save
|
|
707
|
+
</button>
|
|
708
|
+
);
|
|
709
|
+
}
|
|
710
|
+
```
|
|
711
|
+
|
|
640
712
|
> Example: [https://codesandbox.io/.../examples/react/mutation](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/mutation)
|
|
641
713
|
|
|
642
714
|
<br><br>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "floppy-disk",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "FloppyDisk - lightweight, simple, and powerful state management library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"state",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"react",
|
|
10
10
|
"hooks",
|
|
11
11
|
"store",
|
|
12
|
-
"utilities"
|
|
12
|
+
"utilities",
|
|
13
|
+
"query"
|
|
13
14
|
],
|
|
14
15
|
"author": "Muhammad Afifudin",
|
|
15
16
|
"license": "MIT",
|
|
@@ -33,22 +34,22 @@
|
|
|
33
34
|
"exports": {
|
|
34
35
|
"./package.json": "./package.json",
|
|
35
36
|
".": {
|
|
36
|
-
"default": "./lib/index.js",
|
|
37
37
|
"types": "./lib/index.d.ts",
|
|
38
|
-
"module": "./esm/index.js",
|
|
39
38
|
"import": {
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
}
|
|
39
|
+
"types": "./esm/index.d.ts",
|
|
40
|
+
"default": "./esm/index.js"
|
|
41
|
+
},
|
|
42
|
+
"module": "./esm/index.js",
|
|
43
|
+
"default": "./lib/index.js"
|
|
43
44
|
},
|
|
44
45
|
"./preact": {
|
|
45
|
-
"default": "./lib/preact/index.js",
|
|
46
46
|
"types": "./lib/preact/index.d.ts",
|
|
47
|
-
"module": "./esm/preact/index.js",
|
|
48
47
|
"import": {
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
}
|
|
48
|
+
"types": "./esm/preact/index.d.ts",
|
|
49
|
+
"default": "./esm/preact/index.js"
|
|
50
|
+
},
|
|
51
|
+
"module": "./esm/preact/index.js",
|
|
52
|
+
"default": "./lib/preact/index.js"
|
|
52
53
|
}
|
|
53
54
|
},
|
|
54
55
|
"scripts": {
|