floppy-disk 2.13.0-beta.1 → 2.14.0-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
CHANGED
|
@@ -9,7 +9,7 @@ Both are awesome state manager. That's why this Floppy Disk library behaves like
|
|
|
9
9
|
|
|
10
10
|
```js
|
|
11
11
|
import { create } from 'zustand'; // 3.3 kB (gzipped: 1.5 kB)
|
|
12
|
-
import { createStore } from 'floppy-disk'; // 1.4 kB (gzipped:
|
|
12
|
+
import { createStore } from 'floppy-disk'; // 1.4 kB (gzipped: 750 B) 🎉
|
|
13
13
|
|
|
14
14
|
import {
|
|
15
15
|
QueryClient,
|
|
@@ -17,8 +17,8 @@ import {
|
|
|
17
17
|
useQuery,
|
|
18
18
|
useInfiniteQuery,
|
|
19
19
|
useMutation,
|
|
20
|
-
} from '@tanstack/react-query'; //
|
|
21
|
-
import { createQuery, createMutation } from 'floppy-disk'; // 9.
|
|
20
|
+
} from '@tanstack/react-query'; // 31.7 kB kB (gzipped: 9.2 kB)
|
|
21
|
+
import { createQuery, createMutation } from 'floppy-disk'; // 9.7 kB (gzipped: 3.3 kB) 🎉
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
- Using Zustand & React-Query: https://demo-zustand-react-query.vercel.app/
|
|
@@ -30,7 +30,7 @@ import { createQuery, createMutation } from 'floppy-disk'; // 9.6 kB (gzipped: 3
|
|
|
30
30
|
|
|
31
31
|
- **Create Store**
|
|
32
32
|
- Get/set store inside/outside component
|
|
33
|
-
-
|
|
33
|
+
- Very simple way to customize the reactivity (state update subscription)
|
|
34
34
|
- Support middleware
|
|
35
35
|
- Set state interception
|
|
36
36
|
- Store event (`onSubscribe`, `onUnsubscribe`, etc.)
|
|
@@ -112,12 +112,12 @@ Use the hook anywhere, no providers are needed.
|
|
|
112
112
|
|
|
113
113
|
```jsx
|
|
114
114
|
function Cat() {
|
|
115
|
-
const
|
|
115
|
+
const age = useCatStore('age');
|
|
116
116
|
return <div>Cat's age: {age}</div>;
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
function Control() {
|
|
120
|
-
const
|
|
120
|
+
const increaseAge = useCatStore('increaseAge');
|
|
121
121
|
return <button onClick={increaseAge}>Increase cat's age</button>;
|
|
122
122
|
}
|
|
123
123
|
```
|
|
@@ -127,29 +127,39 @@ function Control() {
|
|
|
127
127
|
Control the reactivity. The concept is same as useEffect dependency array.
|
|
128
128
|
|
|
129
129
|
```jsx
|
|
130
|
-
function
|
|
130
|
+
function YourComponent() {
|
|
131
131
|
const { age, isSleeping } = useCatStore();
|
|
132
132
|
// Will re-render every state change ^
|
|
133
|
-
return <div>...</div>;
|
|
134
133
|
}
|
|
135
134
|
|
|
136
|
-
function
|
|
135
|
+
function YourComponent() {
|
|
137
136
|
const { age, isSleeping } = useCatStore((state) => [state.isSleeping]);
|
|
138
137
|
// Will only re-render when isSleeping is updated ^
|
|
139
138
|
// Update on age won't cause re-render this component
|
|
140
|
-
return <div>...</div>;
|
|
141
139
|
}
|
|
142
140
|
|
|
143
|
-
function
|
|
141
|
+
function YourComponent() {
|
|
144
142
|
const { age, isSleeping } = useCatStore((state) => [state.age, state.isSleeping]);
|
|
145
143
|
// Will re-render when age or isSleeping is updated ^
|
|
146
|
-
return <div>...</div>;
|
|
147
144
|
}
|
|
148
145
|
|
|
149
|
-
function
|
|
146
|
+
function YourComponent() {
|
|
150
147
|
const { age, isSleeping } = useCatStore((state) => [state.age > 3]);
|
|
151
148
|
// Will only re-render when (age>3) is updated
|
|
152
|
-
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Even simpler way, after version `2.13.0`, we can use store's object key:
|
|
153
|
+
|
|
154
|
+
```jsx
|
|
155
|
+
function YourComponent() {
|
|
156
|
+
const age = useCatStore('age');
|
|
157
|
+
// Will only re-render when age is updated
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function YourComponent() {
|
|
161
|
+
const age = useCatStore('isSleeping');
|
|
162
|
+
// Will only re-render when isSleeping is updated
|
|
153
163
|
}
|
|
154
164
|
```
|
|
155
165
|
|
|
@@ -188,7 +198,7 @@ const decreaseAgeSilently = () => {
|
|
|
188
198
|
};
|
|
189
199
|
// 👇 Will not re-render
|
|
190
200
|
function Cat() {
|
|
191
|
-
const
|
|
201
|
+
const age = useCatStore('age');
|
|
192
202
|
return <div>Cat's age: {age}</div>;
|
|
193
203
|
}
|
|
194
204
|
```
|
|
@@ -258,7 +268,7 @@ Prevent re-render using `Watch`.
|
|
|
258
268
|
|
|
259
269
|
```jsx
|
|
260
270
|
function CatPage() {
|
|
261
|
-
const
|
|
271
|
+
const age = useCatStore('age');
|
|
262
272
|
// If age changed, this component will re-render which will cause
|
|
263
273
|
// HeavyComponent1 & HeavyComponent2 to be re-rendered as well.
|
|
264
274
|
return (
|
|
@@ -276,8 +286,8 @@ function CatPageOptimized() {
|
|
|
276
286
|
<main>
|
|
277
287
|
<HeavyComponent1 />
|
|
278
288
|
<useCatStore.Watch
|
|
279
|
-
selectDeps=
|
|
280
|
-
render={(
|
|
289
|
+
selectDeps="age"
|
|
290
|
+
render={(age) => {
|
|
281
291
|
return <div>Cat's age: {age}</div>;
|
|
282
292
|
}}
|
|
283
293
|
/>
|
|
@@ -328,11 +338,20 @@ function Parent() {
|
|
|
328
338
|
|
|
329
339
|
function CatAge() {
|
|
330
340
|
const { age } = useCatStoreContext()((state) => [state.age]);
|
|
341
|
+
|
|
342
|
+
// Shorthand after v1.13.0:
|
|
343
|
+
// const age = useCatStoreContext()('age');
|
|
344
|
+
|
|
331
345
|
return <div>Age: {age}</div>;
|
|
332
346
|
}
|
|
347
|
+
|
|
333
348
|
function CatIsSleeping() {
|
|
334
349
|
const useCatStore = useCatStoreContext();
|
|
335
350
|
const { isSleeping } = useCatStore((state) => [state.isSleeping]);
|
|
351
|
+
|
|
352
|
+
// Shorthand after v1.13.0:
|
|
353
|
+
// const isSleeping = useCatStore('isSleeping');
|
|
354
|
+
|
|
336
355
|
return (
|
|
337
356
|
<>
|
|
338
357
|
<div>Is Sleeping: {String(isSleeping)}</div>
|
|
@@ -764,7 +783,7 @@ function SaveProduct() {
|
|
|
764
783
|
|
|
765
784
|
## Important Notes
|
|
766
785
|
|
|
767
|
-
Don't mutate.
|
|
786
|
+
Don't mutate. (unless you use Immer JS library or something similar)
|
|
768
787
|
|
|
769
788
|
```js
|
|
770
789
|
import { createStore } from 'floppy-disk';
|
|
@@ -783,6 +802,7 @@ Don't use conditional reactivity selector.
|
|
|
783
802
|
|
|
784
803
|
```jsx
|
|
785
804
|
function Cat({ isSomething }) {
|
|
805
|
+
const value = useCatStore(isSomething ? 'age' : 'isSleeping'); // ❌
|
|
786
806
|
const { age } = useCatStore(isSomething ? (state) => [state.age] : null); // ❌
|
|
787
807
|
const { age } = useCatStore((state) => (isSomething ? [state.age] : [state.isSleeping])); // ❌
|
|
788
808
|
return <div>Cat's age: {age}</div>;
|
|
@@ -4,7 +4,7 @@ import { createStore } from './create-store';
|
|
|
4
4
|
* @see https://floppy-disk.vercel.app/docs/api#createmutation
|
|
5
5
|
*/
|
|
6
6
|
export const createMutation = (mutationFn, options = {}) => {
|
|
7
|
-
const { onMutate = noop, onSuccess = noop, onError =
|
|
7
|
+
const { onMutate = noop, onSuccess = noop, onError = console.error, onSettled = noop, ...createStoreOptions } = options;
|
|
8
8
|
const useMutation = createStore(({ set, get }) => ({
|
|
9
9
|
isWaiting: false,
|
|
10
10
|
isSuccess: false,
|
|
@@ -40,7 +40,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
40
40
|
const defaultFetchOnReconnect = options.fetchOnMount ?? true;
|
|
41
41
|
const { onFirstSubscribe = noop, onSubscribe = noop, onLastUnsubscribe = noop, onBeforeChangeKey = noop, defaultDeps = useQueryDefaultDeps, select = identityFn, staleTime = 3000, // 3 seconds
|
|
42
42
|
fetchOnMount = true, fetchOnWindowFocus = defaultFetchOnWindowFocus, fetchOnReconnect = defaultFetchOnReconnect, enabled = true, retry = 1, retryDelay = 2000, // 2 seconds
|
|
43
|
-
keepPreviousData, getNextPageParam = () => undefined, onSuccess = noop, onError =
|
|
43
|
+
keepPreviousData, getNextPageParam = () => undefined, onSuccess = noop, onError = console.error, onSettled = noop, cacheTime = 5 * 60 * 1000, refetchInterval = false, ...createStoresOptions } = options;
|
|
44
44
|
const retryTimeoutId = new Map();
|
|
45
45
|
const retryNextPageTimeoutId = new Map();
|
|
46
46
|
const resetTimeoutId = new Map();
|
|
@@ -7,7 +7,7 @@ const create_store_1 = require("./create-store");
|
|
|
7
7
|
* @see https://floppy-disk.vercel.app/docs/api#createmutation
|
|
8
8
|
*/
|
|
9
9
|
const createMutation = (mutationFn, options = {}) => {
|
|
10
|
-
const { onMutate = utils_1.noop, onSuccess = utils_1.noop, onError =
|
|
10
|
+
const { onMutate = utils_1.noop, onSuccess = utils_1.noop, onError = console.error, onSettled = utils_1.noop, ...createStoreOptions } = options;
|
|
11
11
|
const useMutation = (0, create_store_1.createStore)(({ set, get }) => ({
|
|
12
12
|
isWaiting: false,
|
|
13
13
|
isSuccess: false,
|
|
@@ -43,7 +43,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
43
43
|
const defaultFetchOnReconnect = options.fetchOnMount ?? true;
|
|
44
44
|
const { onFirstSubscribe = utils_1.noop, onSubscribe = utils_1.noop, onLastUnsubscribe = utils_1.noop, onBeforeChangeKey = utils_1.noop, defaultDeps = useQueryDefaultDeps, select = utils_1.identityFn, staleTime = 3000, // 3 seconds
|
|
45
45
|
fetchOnMount = true, fetchOnWindowFocus = defaultFetchOnWindowFocus, fetchOnReconnect = defaultFetchOnReconnect, enabled = true, retry = 1, retryDelay = 2000, // 2 seconds
|
|
46
|
-
keepPreviousData, getNextPageParam = () => undefined, onSuccess = utils_1.noop, onError =
|
|
46
|
+
keepPreviousData, getNextPageParam = () => undefined, onSuccess = utils_1.noop, onError = console.error, onSettled = utils_1.noop, cacheTime = 5 * 60 * 1000, refetchInterval = false, ...createStoresOptions } = options;
|
|
47
47
|
const retryTimeoutId = new Map();
|
|
48
48
|
const retryNextPageTimeoutId = new Map();
|
|
49
49
|
const resetTimeoutId = new Map();
|