@t8/react-pending 1.0.17 → 1.0.18

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.
Files changed (2) hide show
  1. package/README.md +24 -13
  2. 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
- No need to rearrange the app's shared state setup and to rewrite the async actions.
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
- ## Example
22
+ ## Shared pending state
12
23
 
13
- Objective: Track the pending state of the async `fetchItems()` action to tell the user whether the UI is busy or encountered an error (preferably without rewriting the action and the app's state management).
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 const ItemList = () => {
19
- const [items, setItems] = useState([]);
20
- + const [state, withState] = usePendingState("fetch-items");
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 const Status = () => {
38
- + const [state] = usePendingState("fetch-items");
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,15 @@ 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
- 🔹 In this example, the action's value (the `items` array) is stored in the component's local state, but it can be stored in any app state of the developer's choice.
61
+ 🔹 In this example, the value returned from the async action, the `items` array, 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 `usePendingState()` is used.
51
62
 
52
- ## Shared and local pending state
63
+ ## Local pending state
53
64
 
54
65
  Omit the custom string key parameter of `usePendingState()` to scope the pending state locally within a single component:
55
66
 
56
67
  ```diff
57
- - const [state, withState] = usePendingState("fetch-items"); // shared
58
- + const [state, withState] = usePendingState(); // local
68
+ - let [state, withState] = usePendingState("fetch-items"); // shared
69
+ + let [state, withState] = usePendingState(); // local
59
70
  ```
60
71
 
61
72
  ## Silent tracking of background and optimistic updates
@@ -101,7 +112,7 @@ Omit the custom string key parameter of `usePendingState()` to scope the pending
101
112
  ## Providing custom initial pending state
102
113
 
103
114
  ```diff
104
- + const initialState = {
115
+ + let initialState = {
105
116
  + "fetch-items": { initialized: true, complete: true },
106
117
  + };
107
118
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/react-pending",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "description": "Concise async action state tracking for React apps",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",