@t8/react-pending 1.0.17 → 1.0.19
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 +26 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,20 +4,31 @@
|
|
|
4
4
|
|
|
5
5
|
*Concise async action state tracking for React apps*
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- Shared or local pending state tracking
|
|
8
|
+
- Without rewrites in the app's shared state or async actions' internals
|
|
9
|
+
- With a concise API
|
|
10
|
+
|
|
11
|
+
```diff
|
|
12
|
+
+ let [state, withState] = usePendingState("fetch-items");
|
|
13
|
+
|
|
14
|
+
- fetchItems().then(setItems);
|
|
15
|
+
+ withState(fetchItems()).then(setItems);
|
|
16
|
+
|
|
17
|
+
+ if (!state.complete) return <p>Loading...</p>;
|
|
18
|
+
```
|
|
8
19
|
|
|
9
20
|
Installation: `npm i @t8/react-pending`
|
|
10
21
|
|
|
11
|
-
##
|
|
22
|
+
## Shared pending state
|
|
12
23
|
|
|
13
|
-
Objective: Track the pending state of the
|
|
24
|
+
Objective: Track the pending state of the asynchronous action `fetchItems()` to tell the user whether the UI is busy or encountered an error (preferably without rewriting the action and the app's state management). In our setup, there are two components rendering their content with regard to the current state of `fetchItems()`.
|
|
14
25
|
|
|
15
26
|
```diff
|
|
16
27
|
+ import { usePendingState } from "@t8/react-pending";
|
|
17
28
|
|
|
18
|
-
export
|
|
19
|
-
|
|
20
|
-
+
|
|
29
|
+
export let ItemList = () => {
|
|
30
|
+
let [items, setItems] = useState([]);
|
|
31
|
+
+ let [state, withState] = usePendingState("fetch-items");
|
|
21
32
|
|
|
22
33
|
useEffect(() => {
|
|
23
34
|
- fetchItems().then(setItems);
|
|
@@ -34,8 +45,8 @@ Objective: Track the pending state of the async `fetchItems()` action to tell th
|
|
|
34
45
|
```diff
|
|
35
46
|
+ import { usePendingState } from "@t8/react-pending";
|
|
36
47
|
|
|
37
|
-
export
|
|
38
|
-
+
|
|
48
|
+
export let Status = () => {
|
|
49
|
+
+ let [state] = usePendingState("fetch-items");
|
|
39
50
|
|
|
40
51
|
if (!state.initialized) return "";
|
|
41
52
|
if (!state.complete) return "Busy";
|
|
@@ -47,15 +58,17 @@ Objective: Track the pending state of the async `fetchItems()` action to tell th
|
|
|
47
58
|
|
|
48
59
|
[Live demo](https://codesandbox.io/p/sandbox/rrr9cl?file=%2Fsrc%2FItemList.tsx)
|
|
49
60
|
|
|
50
|
-
🔹
|
|
61
|
+
🔹 To share the async action's pending state with multiple components we're using the string key parameter of `usePendingState(stateKey)`. This key can be used with `usePendingState(stateKey)` in other components to refer to the same pending state (as in the `Status` component above), so `stateKey` should be unique to the particular pending state.
|
|
62
|
+
|
|
63
|
+
🔹 In the example above, the data returned from the async action is stored in the component's local state, but it can be stored in any app state of the developer's choice without affecting how the `usePendingState()` hook is used.
|
|
51
64
|
|
|
52
|
-
##
|
|
65
|
+
## Local pending state
|
|
53
66
|
|
|
54
67
|
Omit the custom string key parameter of `usePendingState()` to scope the pending state locally within a single component:
|
|
55
68
|
|
|
56
69
|
```diff
|
|
57
|
-
-
|
|
58
|
-
+
|
|
70
|
+
- let [state, withState] = usePendingState("fetch-items"); // shared
|
|
71
|
+
+ let [state, withState] = usePendingState(); // local
|
|
59
72
|
```
|
|
60
73
|
|
|
61
74
|
## Silent tracking of background and optimistic updates
|
|
@@ -101,7 +114,7 @@ Omit the custom string key parameter of `usePendingState()` to scope the pending
|
|
|
101
114
|
## Providing custom initial pending state
|
|
102
115
|
|
|
103
116
|
```diff
|
|
104
|
-
+
|
|
117
|
+
+ let initialState = {
|
|
105
118
|
+ "fetch-items": { initialized: true, complete: true },
|
|
106
119
|
+ };
|
|
107
120
|
|