@t8/react-pending 1.0.16 → 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.
- package/LICENSE +21 -0
- package/README.md +24 -13
- package/package.json +5 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Alexander Tkačenko
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
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,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
|
|
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
|
-
##
|
|
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
|
-
-
|
|
58
|
-
+
|
|
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
|
-
+
|
|
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.
|
|
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",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"typecheck": "tsc --noEmit"
|
|
17
17
|
},
|
|
18
18
|
"author": "axtk",
|
|
19
|
-
"license": "
|
|
19
|
+
"license": "MIT",
|
|
20
20
|
"repository": {
|
|
21
21
|
"type": "git",
|
|
22
22
|
"url": "git+https://github.com/t8js/react-pending.git"
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"react": ">=16.8"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@playwright/test": "^1.
|
|
35
|
-
"@t8/serve": "^0.1.
|
|
34
|
+
"@playwright/test": "^1.56.0",
|
|
35
|
+
"@t8/serve": "^0.1.30",
|
|
36
36
|
"@types/node": "^24.5.2",
|
|
37
37
|
"@types/react": "^19.1.10",
|
|
38
38
|
"@types/react-dom": "^19.1.9",
|
|
@@ -40,6 +40,6 @@
|
|
|
40
40
|
"typescript": "^5.9.3"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@t8/react-store": "^1.0.
|
|
43
|
+
"@t8/react-store": "^1.0.27"
|
|
44
44
|
}
|
|
45
45
|
}
|