floppy-disk 1.4.0-beta.2 → 1.4.1-beta.1
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 +36 -21
- package/package.json +1 -4
package/README.md
CHANGED
|
@@ -8,8 +8,7 @@ This library was highly-inspired by [Zustand](https://www.npmjs.com/package/zust
|
|
|
8
8
|
|
|
9
9
|
```js
|
|
10
10
|
import { create } from 'zustand'; // 3.3 kB (gzipped: 1.5 kB)
|
|
11
|
-
|
|
12
|
-
import { createStore } from 'floppy-disk'; // 1.1 kB (gzipped: 622 B) 🎉
|
|
11
|
+
import { createStore } from 'floppy-disk'; // 1.3 kB (gzipped: 702 B) 🎉
|
|
13
12
|
|
|
14
13
|
import {
|
|
15
14
|
QueryClient,
|
|
@@ -18,14 +17,13 @@ import {
|
|
|
18
17
|
useInfiniteQuery,
|
|
19
18
|
useMutation,
|
|
20
19
|
} from '@tanstack/react-query'; // 41 kB (gzipped: 11 kB)
|
|
21
|
-
|
|
22
|
-
import { createQuery, createMutation } from 'floppy-disk'; // 6.4 kB (gzipped: 2.3 kB) 🎉
|
|
20
|
+
import { createQuery, createMutation } from 'floppy-disk'; // 7.5 kB (gzipped: 2.6 kB) 🎉
|
|
23
21
|
```
|
|
24
22
|
|
|
25
23
|
- Using Zustand & React-Query: https://demo-zustand-react-query.vercel.app/
|
|
26
|
-
👉 Total: **309.22 kB**
|
|
24
|
+
👉 Total: **309.22 kB**
|
|
27
25
|
- Using Floppy Disk: https://demo-floppy-disk.vercel.app/
|
|
28
|
-
👉 Total: **
|
|
26
|
+
👉 Total: **282.86 kB** 🎉
|
|
29
27
|
|
|
30
28
|
## Table of Contents
|
|
31
29
|
|
|
@@ -71,6 +69,8 @@ function Control() {
|
|
|
71
69
|
}
|
|
72
70
|
```
|
|
73
71
|
|
|
72
|
+
> Example: [https://codesandbox.io/.../examples/react/basic](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/basic)
|
|
73
|
+
|
|
74
74
|
Control the reactivity. The concept is same as useEffect dependency array.
|
|
75
75
|
|
|
76
76
|
```jsx
|
|
@@ -100,6 +100,8 @@ function Cat() {
|
|
|
100
100
|
}
|
|
101
101
|
```
|
|
102
102
|
|
|
103
|
+
> Example: [https://codesandbox.io/.../examples/react/custom-reactivity](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/custom-reactivity)
|
|
104
|
+
|
|
103
105
|
Reading/writing state and reacting to changes outside of components.
|
|
104
106
|
|
|
105
107
|
```js
|
|
@@ -122,11 +124,6 @@ const unsub = useCatStore.subscribe(
|
|
|
122
124
|
);
|
|
123
125
|
```
|
|
124
126
|
|
|
125
|
-
> **Examples:**
|
|
126
|
-
>
|
|
127
|
-
> - [https://codesandbox.io/.../examples/react/basic](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/basic)
|
|
128
|
-
> - [https://codesandbox.io/.../examples/react/custom-reactivity](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/custom-reactivity)
|
|
129
|
-
|
|
130
127
|
### Advanced Concept
|
|
131
128
|
|
|
132
129
|
Set the state **silently** (without broadcast the state change to **any subscribers**).
|
|
@@ -176,10 +173,9 @@ const useCatStore = createStore(
|
|
|
176
173
|
);
|
|
177
174
|
```
|
|
178
175
|
|
|
179
|
-
>
|
|
180
|
-
>
|
|
181
|
-
>
|
|
182
|
-
> - [https://codesandbox.io/.../examples/react/intercept](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/intercept)
|
|
176
|
+
> Example:
|
|
177
|
+
> [https://codesandbox.io/.../examples/react/store-event](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/store-event)
|
|
178
|
+
> [https://codesandbox.io/.../examples/react/intercept](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/intercept)
|
|
183
179
|
|
|
184
180
|
Let's go wild using IIFE.
|
|
185
181
|
|
|
@@ -238,9 +234,7 @@ function CatPageOptimized() {
|
|
|
238
234
|
}
|
|
239
235
|
```
|
|
240
236
|
|
|
241
|
-
>
|
|
242
|
-
>
|
|
243
|
-
> - [https://codesandbox.io/.../examples/react/watch-component](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/watch-component)
|
|
237
|
+
> Example: [https://codesandbox.io/.../examples/react/watch-component](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/watch-component)
|
|
244
238
|
|
|
245
239
|
Want a local state instead of global state?
|
|
246
240
|
Or, want to set the initial state inside component?
|
|
@@ -289,12 +283,14 @@ function CatIsSleeping() {
|
|
|
289
283
|
return (
|
|
290
284
|
<>
|
|
291
285
|
<div>Is Sleeping: {String(isSleeping)}</div>
|
|
292
|
-
<button onClick={useCatStore.get().
|
|
286
|
+
<button onClick={useCatStore.get().increaseAge}>Increase cat age</button>
|
|
293
287
|
</>
|
|
294
288
|
);
|
|
295
289
|
}
|
|
296
290
|
```
|
|
297
291
|
|
|
292
|
+
> Example: [https://codesandbox.io/.../examples/react/local-state](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/local-state)
|
|
293
|
+
|
|
298
294
|
Set default reactivity.
|
|
299
295
|
|
|
300
296
|
```jsx
|
|
@@ -338,6 +334,7 @@ const useCatStores = createStores(
|
|
|
338
334
|
onBeforeChangeKey: (nextKey, prevKey) => {
|
|
339
335
|
console.log('Store key changed', nextKey, prevKey);
|
|
340
336
|
},
|
|
337
|
+
// ... same as createStore
|
|
341
338
|
},
|
|
342
339
|
);
|
|
343
340
|
|
|
@@ -367,6 +364,8 @@ function Control({ catId }) {
|
|
|
367
364
|
}
|
|
368
365
|
```
|
|
369
366
|
|
|
367
|
+
> Example: [https://codesandbox.io/.../examples/react/stores](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/stores)
|
|
368
|
+
|
|
370
369
|
## Query & Mutation
|
|
371
370
|
|
|
372
371
|
With the power of `createStores` function and a bit creativity, we can easily create a hook just like `useQuery` and `useInfiniteQuery` in [React-Query](https://tanstack.com/query) using `createQuery` function.
|
|
@@ -398,6 +397,8 @@ function SingleQuery() {
|
|
|
398
397
|
}
|
|
399
398
|
```
|
|
400
399
|
|
|
400
|
+
> Example: [https://codesandbox.io/.../examples/react/query](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/query)
|
|
401
|
+
|
|
401
402
|
Custom reactivity:
|
|
402
403
|
|
|
403
404
|
```jsx
|
|
@@ -492,6 +493,8 @@ function PokemonPage() {
|
|
|
492
493
|
}
|
|
493
494
|
```
|
|
494
495
|
|
|
496
|
+
> Example: [https://codesandbox.io/.../examples/react/query-with-param](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/query-with-param)
|
|
497
|
+
|
|
495
498
|
Get data or do something outside component:
|
|
496
499
|
|
|
497
500
|
```jsx
|
|
@@ -549,11 +552,14 @@ function PokemonListPage() {
|
|
|
549
552
|
}
|
|
550
553
|
```
|
|
551
554
|
|
|
555
|
+
> Example: [https://codesandbox.io/.../examples/react/infinite-query](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/infinite-query)
|
|
556
|
+
|
|
552
557
|
**Note:**
|
|
553
558
|
|
|
554
559
|
- The default stale time is 3 seconds.
|
|
555
|
-
- The default
|
|
556
|
-
|
|
560
|
+
- The default error retry attempt is 1 time, and retry delay is 3 seconds.
|
|
561
|
+
- The default reactivity of a query is `(s) => [s.data, s.error]`.
|
|
562
|
+
(For paginated: `(s) => [s.data, s.error, s.isWaitingNextPage, s.hasNextPage]`)
|
|
557
563
|
- You can change the `defaultDeps` on `createQuery` options.
|
|
558
564
|
|
|
559
565
|
### Mutation
|
|
@@ -599,6 +605,15 @@ function Login() {
|
|
|
599
605
|
}
|
|
600
606
|
```
|
|
601
607
|
|
|
608
|
+
> Example: [https://codesandbox.io/.../examples/react/mutation](https://codesandbox.io/p/sandbox/github/afiiif/floppy-disk/tree/main/examples/react/mutation)
|
|
609
|
+
|
|
610
|
+
<br><br>
|
|
611
|
+
|
|
612
|
+
<p align="center">
|
|
613
|
+
— ✨ 💾 ✨ —
|
|
614
|
+
</p>
|
|
615
|
+
<br>
|
|
616
|
+
|
|
602
617
|
## Important Notes
|
|
603
618
|
|
|
604
619
|
Don't mutate.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "floppy-disk",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.1-beta.1",
|
|
4
4
|
"description": "FloppyDisk - lightweight, simple, and powerful state management library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"state",
|
|
@@ -30,9 +30,6 @@
|
|
|
30
30
|
],
|
|
31
31
|
"types": "lib/index.d.ts",
|
|
32
32
|
"typings": "lib/index.d.ts",
|
|
33
|
-
"engines": {
|
|
34
|
-
"node": ">=14"
|
|
35
|
-
},
|
|
36
33
|
"scripts": {
|
|
37
34
|
"prepare": "husky install",
|
|
38
35
|
"build:cjs": "tsc -p tsconfig.prod.json",
|