@anjianshi/utils 2.8.3 → 2.8.5
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/env-react/hooks.d.ts +1 -5
- package/env-react/hooks.js +11 -12
- package/package.json +2 -2
package/env-react/hooks.d.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 生成一个 ref
|
|
3
|
-
*/
|
|
4
|
-
export declare function useRefOfValue<T>(value: T): import("react").RefObject<T>;
|
|
5
|
-
/**
|
|
6
|
-
* 在 useState() 基础上,额外返回一个与 state 值同步的 ref
|
|
2
|
+
* 生成一个 state 以及与其值同步的 ref
|
|
7
3
|
*/
|
|
8
4
|
export declare function useStateWithRef<T>(initialValue: T | (() => T)): readonly [T, import("react").Dispatch<import("react").SetStateAction<T>>, import("react").RefObject<T>];
|
package/env-react/hooks.js
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import { useRef } from 'react';
|
|
1
|
+
import { useRef, useCallback } from 'react';
|
|
2
2
|
/**
|
|
3
|
-
* 生成一个 ref
|
|
4
|
-
*/
|
|
5
|
-
export function useRefOfValue(value) {
|
|
6
|
-
const ref = useRef(value);
|
|
7
|
-
ref.current = value;
|
|
8
|
-
return ref;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* 在 useState() 基础上,额外返回一个与 state 值同步的 ref
|
|
3
|
+
* 生成一个 state 以及与其值同步的 ref
|
|
12
4
|
*/
|
|
13
5
|
export function useStateWithRef(initialValue) {
|
|
14
6
|
const [state, setState] = useState(initialValue);
|
|
15
|
-
const ref =
|
|
16
|
-
|
|
7
|
+
const ref = useRef(state);
|
|
8
|
+
const setStateWithRef = useCallback((value) => {
|
|
9
|
+
setState(prevState => {
|
|
10
|
+
const newValue = typeof value === 'function' ? value(prevState) : value;
|
|
11
|
+
ref.current = newValue;
|
|
12
|
+
return newValue;
|
|
13
|
+
});
|
|
14
|
+
}, []);
|
|
15
|
+
return [state, setStateWithRef, ref];
|
|
17
16
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anjianshi/utils",
|
|
3
|
-
"version": "2.8.
|
|
3
|
+
"version": "2.8.5",
|
|
4
4
|
"description": "Common JavaScript Utils",
|
|
5
5
|
"homepage": "https://github.com/anjianshi/js-packages/utils",
|
|
6
6
|
"bugs": {
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"@anjianshi/presets-eslint-base": "6.0.0",
|
|
35
35
|
"@anjianshi/presets-eslint-node": "6.0.0",
|
|
36
36
|
"@anjianshi/presets-eslint-react": "6.0.0",
|
|
37
|
-
"@anjianshi/presets-prettier": "3.0.4",
|
|
38
37
|
"@anjianshi/presets-eslint-typescript": "6.0.0",
|
|
38
|
+
"@anjianshi/presets-prettier": "3.0.4",
|
|
39
39
|
"@anjianshi/presets-typescript": "3.2.4"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|