@t8/react-pending 1.0.30 → 1.0.32
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 +13 -13
- package/dist/index.cjs +2 -2
- package/dist/index.d.ts +4 -4
- package/dist/index.mjs +2 -2
- package/package.json +6 -6
- package/src/usePendingState.ts +4 -4
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# T8 React Pending
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A concise async action state management lib for React apps
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@t8/react-pending)   
|
|
6
6
|
|
|
@@ -60,19 +60,19 @@ In our setup, there are two components rendering their content with regard to th
|
|
|
60
60
|
export let Status = () => {
|
|
61
61
|
+ let [state] = usePendingState("fetch-items");
|
|
62
62
|
|
|
63
|
-
if (!state.initialized) return
|
|
64
|
-
if (!state.complete) return
|
|
65
|
-
if (state.error) return
|
|
63
|
+
if (!state.initialized) return null;
|
|
64
|
+
if (!state.complete) return <>Busy</>;
|
|
65
|
+
if (state.error) return <>Error</>;
|
|
66
66
|
|
|
67
|
-
return
|
|
67
|
+
return <>OK</>;
|
|
68
68
|
};
|
|
69
69
|
```
|
|
70
70
|
|
|
71
71
|
[Live demo](https://codesandbox.io/p/sandbox/rrr9cl?file=%2Fsrc%2FItemList.tsx)
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
⬥ 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.
|
|
74
74
|
|
|
75
|
-
|
|
75
|
+
⬥ 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.
|
|
76
76
|
|
|
77
77
|
## Local pending state
|
|
78
78
|
|
|
@@ -90,7 +90,7 @@ Omit the custom string key parameter of `usePendingState()` to scope the pending
|
|
|
90
90
|
+ withState(fetchItems(), { silent: true })
|
|
91
91
|
```
|
|
92
92
|
|
|
93
|
-
|
|
93
|
+
⬥ This option prevents `state.complete` from switching to `false` in the pending state.
|
|
94
94
|
|
|
95
95
|
## Delayed pending state
|
|
96
96
|
|
|
@@ -99,7 +99,7 @@ Omit the custom string key parameter of `usePendingState()` to scope the pending
|
|
|
99
99
|
+ withState(fetchItems(), { delay: 500 }) // in milliseconds
|
|
100
100
|
```
|
|
101
101
|
|
|
102
|
-
|
|
102
|
+
⬥ Use case: avoiding flashing a process indicator when the action is likely to complete by the end of a short delay.
|
|
103
103
|
|
|
104
104
|
## Custom rejection handler
|
|
105
105
|
|
|
@@ -108,7 +108,7 @@ Omit the custom string key parameter of `usePendingState()` to scope the pending
|
|
|
108
108
|
+ withState(fetchItems(), { throws: true }).catch(handleError)
|
|
109
109
|
```
|
|
110
110
|
|
|
111
|
-
|
|
111
|
+
⬥ This option allows the async action to reject explicitly, along with exposing `state.error` that goes by default.
|
|
112
112
|
|
|
113
113
|
## Providing blank initial pending state
|
|
114
114
|
|
|
@@ -121,7 +121,7 @@ Omit the custom string key parameter of `usePendingState()` to scope the pending
|
|
|
121
121
|
+ </PendingStateProvider>
|
|
122
122
|
```
|
|
123
123
|
|
|
124
|
-
|
|
124
|
+
⬥ `<PendingStateProvider>` creates an isolated instance of initial shared action state. Its prime use cases are SSR and tests. It isn't required with client-side rendering, but it can be used to separate action states of larger self-contained portions of an app.
|
|
125
125
|
|
|
126
126
|
## Providing custom initial pending state
|
|
127
127
|
|
|
@@ -136,6 +136,6 @@ Omit the custom string key parameter of `usePendingState()` to scope the pending
|
|
|
136
136
|
</PendingStateProvider>
|
|
137
137
|
```
|
|
138
138
|
|
|
139
|
-
|
|
139
|
+
⬥ While fully optional, this setup allows to redefine the initial state received from `usePendingState(stateKey)`.
|
|
140
140
|
|
|
141
|
-
|
|
141
|
+
⬥ With an explicit value or without, the `<PendingStateProvider>`'s nested components will only respond to updates in the particular action states they subscribed to by means of `usePendingState(stateKey)`.
|
package/dist/index.cjs
CHANGED
|
@@ -37,8 +37,8 @@ function createState(initialized = false, complete = false, error) {
|
|
|
37
37
|
* - `state` is the current value of the action's state;
|
|
38
38
|
* - `withState(action, options?)` reads and tracks the `actions`'s state
|
|
39
39
|
* which is exposed as `state` listed above;
|
|
40
|
-
* - `setState(update)` can replace the current `state` value
|
|
41
|
-
* an another state value.
|
|
40
|
+
* - `setState(update)` can be used to replace the current `state` value
|
|
41
|
+
* directly with an another state value.
|
|
42
42
|
*/
|
|
43
43
|
function usePendingState(store) {
|
|
44
44
|
let storeMap = (0, react.useContext)(PendingStateContext);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as react0 from "react";
|
|
2
2
|
import { ReactNode } from "react";
|
|
3
|
-
import {
|
|
3
|
+
import { SetStoreValue, Store } from "@t8/react-store";
|
|
4
4
|
import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
5
5
|
|
|
6
6
|
type PendingState = {
|
|
@@ -55,9 +55,9 @@ type WithStateOptions = {
|
|
|
55
55
|
* - `state` is the current value of the action's state;
|
|
56
56
|
* - `withState(action, options?)` reads and tracks the `actions`'s state
|
|
57
57
|
* which is exposed as `state` listed above;
|
|
58
|
-
* - `setState(update)` can replace the current `state` value
|
|
59
|
-
* an another state value.
|
|
58
|
+
* - `setState(update)` can be used to replace the current `state` value
|
|
59
|
+
* directly with an another state value.
|
|
60
60
|
*/
|
|
61
|
-
declare function usePendingState(store?: string | Store<PendingState> | null): [PendingState, <T>(value: T) => T,
|
|
61
|
+
declare function usePendingState(store?: string | Store<PendingState> | null): [PendingState, <T>(value: T) => T, SetStoreValue<PendingState>];
|
|
62
62
|
|
|
63
63
|
export { PendingState, PendingStateContext, PendingStateProvider, PendingStateProviderProps, WithStateOptions, usePendingState };
|
package/dist/index.mjs
CHANGED
|
@@ -37,8 +37,8 @@ function createState(initialized = false, complete = false, error) {
|
|
|
37
37
|
* - `state` is the current value of the action's state;
|
|
38
38
|
* - `withState(action, options?)` reads and tracks the `actions`'s state
|
|
39
39
|
* which is exposed as `state` listed above;
|
|
40
|
-
* - `setState(update)` can replace the current `state` value
|
|
41
|
-
* an another state value.
|
|
40
|
+
* - `setState(update)` can be used to replace the current `state` value
|
|
41
|
+
* directly with an another state value.
|
|
42
42
|
*/
|
|
43
43
|
function usePendingState(store) {
|
|
44
44
|
let storeMap = useContext(PendingStateContext);
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t8/react-pending",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.32",
|
|
4
|
+
"description": "A concise async action state management lib for React apps",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"scripts": {
|
|
10
|
-
"demo": "npx @t8/serve
|
|
10
|
+
"demo": "npx @t8/serve tests/async_status --spa -b src/index.tsx",
|
|
11
11
|
"preversion": "npx npm-run-all shape test",
|
|
12
12
|
"shape": "npx codeshape",
|
|
13
13
|
"test": "npx playwright test --project=chromium"
|
|
@@ -29,13 +29,13 @@
|
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@playwright/test": "^1.56.0",
|
|
32
|
-
"@t8/serve": "^0.
|
|
32
|
+
"@t8/serve": "^0.2.0",
|
|
33
33
|
"@types/node": "^24.10.2",
|
|
34
34
|
"@types/react": "^19.2.7",
|
|
35
35
|
"@types/react-dom": "^19.2.3",
|
|
36
|
-
"react-dom": "^19.2.
|
|
36
|
+
"react-dom": "^19.2.3"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@t8/react-store": "^1.2.
|
|
39
|
+
"@t8/react-store": "^1.2.7"
|
|
40
40
|
}
|
|
41
41
|
}
|
package/src/usePendingState.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isStore, type
|
|
1
|
+
import { isStore, type SetStoreValue, Store, useStore } from "@t8/react-store";
|
|
2
2
|
import { useCallback, useContext, useMemo, useRef, useState } from "react";
|
|
3
3
|
import type { PendingState } from "./PendingState.ts";
|
|
4
4
|
import { PendingStateContext } from "./PendingStateContext.ts";
|
|
@@ -51,12 +51,12 @@ export type WithStateOptions = {
|
|
|
51
51
|
* - `state` is the current value of the action's state;
|
|
52
52
|
* - `withState(action, options?)` reads and tracks the `actions`'s state
|
|
53
53
|
* which is exposed as `state` listed above;
|
|
54
|
-
* - `setState(update)` can replace the current `state` value
|
|
55
|
-
* an another state value.
|
|
54
|
+
* - `setState(update)` can be used to replace the current `state` value
|
|
55
|
+
* directly with an another state value.
|
|
56
56
|
*/
|
|
57
57
|
export function usePendingState(
|
|
58
58
|
store?: string | Store<PendingState> | null,
|
|
59
|
-
): [PendingState, <T>(value: T) => T,
|
|
59
|
+
): [PendingState, <T>(value: T) => T, SetStoreValue<PendingState>] {
|
|
60
60
|
let storeMap = useContext(PendingStateContext);
|
|
61
61
|
let storeRef = useRef<Store<PendingState> | null>(null);
|
|
62
62
|
let [storeItemInited, setStoreItemInited] = useState(false);
|