@etsoo/react 1.5.97 → 1.5.99
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/__tests__/States.tsx +5 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/uses/useAsyncState.d.ts +4 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/uses/useAsyncState.ts +24 -4
package/__tests__/States.tsx
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { render, screen
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
3
|
import { useAsyncState } from '../src/uses/useAsyncState';
|
|
4
4
|
import { act } from 'react-dom/test-utils';
|
|
5
5
|
|
|
6
6
|
function App(props: { callback: (state: number) => void }) {
|
|
7
7
|
const { callback } = props;
|
|
8
8
|
const [state, setState] = useAsyncState(0);
|
|
9
|
+
|
|
10
|
+
const [state1] = useAsyncState<number>();
|
|
11
|
+
expect(state1).toBeUndefined();
|
|
12
|
+
|
|
9
13
|
const click = async () => {
|
|
10
14
|
const currentState = await setState((prev) => prev + 1);
|
|
11
15
|
callback(currentState + 1);
|
package/lib/index.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ export * from './states/IState';
|
|
|
16
16
|
export * from './states/PageState';
|
|
17
17
|
export * from './states/State';
|
|
18
18
|
export * from './states/UserState';
|
|
19
|
+
export * from './uses/useAsyncState';
|
|
19
20
|
export * from './uses/useCombinedRefs';
|
|
20
21
|
export * from './uses/useDelayedExecutor';
|
|
21
22
|
export * from './uses/useDimensions';
|
package/lib/index.js
CHANGED
|
@@ -21,6 +21,7 @@ export * from './states/PageState';
|
|
|
21
21
|
export * from './states/State';
|
|
22
22
|
export * from './states/UserState';
|
|
23
23
|
// uses
|
|
24
|
+
export * from './uses/useAsyncState';
|
|
24
25
|
export * from './uses/useCombinedRefs';
|
|
25
26
|
export * from './uses/useDelayedExecutor';
|
|
26
27
|
export * from './uses/useDimensions';
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
export declare function useAsyncState<S = undefined>(): [
|
|
3
|
+
S | undefined,
|
|
4
|
+
(newState: React.SetStateAction<S | undefined>) => Promise<S | undefined>
|
|
5
|
+
];
|
|
2
6
|
/**
|
|
3
7
|
* Returns a stateful value, and a async function to update it.
|
|
4
8
|
* @param initialState initial stat
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
|
|
3
|
+
export function useAsyncState<S = undefined>(): [
|
|
4
|
+
S | undefined,
|
|
5
|
+
(newState: React.SetStateAction<S | undefined>) => Promise<S | undefined>
|
|
6
|
+
];
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* Returns a stateful value, and a async function to update it.
|
|
5
10
|
* @param initialState initial stat
|
|
@@ -7,12 +12,27 @@ import React from 'react';
|
|
|
7
12
|
*/
|
|
8
13
|
export function useAsyncState<S>(
|
|
9
14
|
initialState: S | (() => S)
|
|
10
|
-
): [S, (newState: React.SetStateAction<S>) => Promise<S>]
|
|
15
|
+
): [S, (newState: React.SetStateAction<S>) => Promise<S>];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Returns a stateful value, and a async function to update it.
|
|
19
|
+
* @param initialState initial stat
|
|
20
|
+
* @returns Current state and update action
|
|
21
|
+
*/
|
|
22
|
+
export function useAsyncState<S>(
|
|
23
|
+
initialState?: S | (() => S)
|
|
24
|
+
): [
|
|
25
|
+
S | undefined,
|
|
26
|
+
(newState: React.SetStateAction<S | undefined>) => Promise<S | undefined>
|
|
27
|
+
] {
|
|
11
28
|
// State
|
|
12
29
|
const [state, setState] = React.useState(initialState);
|
|
13
30
|
|
|
14
31
|
// Resolve sate
|
|
15
|
-
const resolveState =
|
|
32
|
+
const resolveState =
|
|
33
|
+
React.useRef<
|
|
34
|
+
(value: S | undefined | PromiseLike<S | undefined>) => void
|
|
35
|
+
>();
|
|
16
36
|
|
|
17
37
|
// Is mounted or not
|
|
18
38
|
const isMounted = React.useRef(false);
|
|
@@ -32,8 +52,8 @@ export function useAsyncState<S>(
|
|
|
32
52
|
}, [state]);
|
|
33
53
|
|
|
34
54
|
const setAsyncState = React.useCallback(
|
|
35
|
-
(newState: React.SetStateAction<S>) =>
|
|
36
|
-
new Promise<S>((resolve) => {
|
|
55
|
+
(newState: React.SetStateAction<S | undefined>) =>
|
|
56
|
+
new Promise<S | undefined>((resolve) => {
|
|
37
57
|
if (isMounted.current) {
|
|
38
58
|
resolveState.current = resolve;
|
|
39
59
|
setState(newState);
|