@pushwoosh/kit-helpers 4.16.0 → 4.16.1
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useInsertionEffect, useRef } from 'react';
|
|
1
|
+
import { useInsertionEffect, useRef, useState } from 'react';
|
|
2
2
|
/**
|
|
3
3
|
* Creates a stable function reference that always calls the latest version of a callback.
|
|
4
4
|
*
|
|
@@ -63,17 +63,10 @@ import { useInsertionEffect, useRef } from 'react';
|
|
|
63
63
|
* };
|
|
64
64
|
*/
|
|
65
65
|
export default function usePersistentFunction(callback) {
|
|
66
|
-
const
|
|
67
|
-
if (holderRef.current === null) {
|
|
68
|
-
const created = {
|
|
69
|
-
latest: callback,
|
|
70
|
-
fn: (...args) => created.latest(...args)
|
|
71
|
-
};
|
|
72
|
-
holderRef.current = created;
|
|
73
|
-
}
|
|
74
|
-
const holder = holderRef.current;
|
|
66
|
+
const latestRef = useRef(callback);
|
|
75
67
|
useInsertionEffect(() => {
|
|
76
|
-
|
|
68
|
+
latestRef.current = callback;
|
|
77
69
|
});
|
|
78
|
-
|
|
70
|
+
const [fn] = useState(() => (...args) => latestRef.current(...args));
|
|
71
|
+
return fn;
|
|
79
72
|
}
|
package/hooks/useUnmount.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { useRef } from 'react';
|
|
1
|
+
import { useLayoutEffect, useRef } from 'react';
|
|
2
2
|
import useEffectOnce from './useEffectOnce';
|
|
3
3
|
/** Call `fn` on unmount; the latest render's `fn` is used, so it needs no stable identity. */
|
|
4
4
|
const useUnmount = fn => {
|
|
5
5
|
const fnRef = useRef(fn);
|
|
6
6
|
// update the ref each render so if it change the newest callback will be invoked
|
|
7
|
-
|
|
7
|
+
useLayoutEffect(() => {
|
|
8
|
+
fnRef.current = fn;
|
|
9
|
+
});
|
|
8
10
|
useEffectOnce(() => () => fnRef.current());
|
|
9
11
|
};
|
|
10
12
|
export default useUnmount;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pushwoosh/kit-helpers",
|
|
3
|
-
"version": "4.16.
|
|
3
|
+
"version": "4.16.1",
|
|
4
4
|
"description": "React component for build Pushwoosh products",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"react-transition-group": "^4.4.5"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@pushwoosh/kit-constants": "^
|
|
17
|
+
"@pushwoosh/kit-constants": "^3.0.0",
|
|
18
18
|
"@types/react": "^19.3.0",
|
|
19
19
|
"@types/react-dom": "^19.3.0",
|
|
20
20
|
"@types/react-transition-group": "^4.4.12",
|