@pushwoosh/kit-helpers 4.12.6 → 4.12.7
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.
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
* Unlike using plain `useCallback`, this approach ensures that the latest version of the
|
|
8
8
|
* callback is always used, even if the reference remains stable.
|
|
9
9
|
*
|
|
10
|
+
* The stored callback is refreshed at commit, so a render prop that calls the returned function while
|
|
11
|
+
* rendering (Slate's `renderElement`) runs the previous render's callback; handlers are unaffected.
|
|
12
|
+
*
|
|
10
13
|
* @param callback - The function that should be persisted
|
|
11
14
|
* @returns A stable function reference that will always call the most recent version of the provided callback
|
|
12
15
|
*
|
|
@@ -43,5 +46,19 @@
|
|
|
43
46
|
*
|
|
44
47
|
* return <div>...</div>;
|
|
45
48
|
* };
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* // Render props run during render, so they see the previous render's callback
|
|
52
|
+
* const MyEditor = () => {
|
|
53
|
+
* const [active, setActive] = useState(true);
|
|
54
|
+
*
|
|
55
|
+
* const renderElement = usePersistentFunction((props) => (
|
|
56
|
+
* <Element {...props} onDelete={() => setActive(false)} />
|
|
57
|
+
* ));
|
|
58
|
+
*
|
|
59
|
+
* // Fine here: the callback closes over `setActive`, which React keeps stable.
|
|
60
|
+
* // Reading `active` inside it would give the previous render's value instead.
|
|
61
|
+
* return <Editable renderElement={renderElement} />;
|
|
62
|
+
* };
|
|
46
63
|
*/
|
|
47
64
|
export default function usePersistentFunction<Args extends unknown[], Return>(callback: (...args: Args) => Return): (...args: Args) => Return;
|
|
@@ -8,6 +8,9 @@ import { useInsertionEffect, useRef } from 'react';
|
|
|
8
8
|
* Unlike using plain `useCallback`, this approach ensures that the latest version of the
|
|
9
9
|
* callback is always used, even if the reference remains stable.
|
|
10
10
|
*
|
|
11
|
+
* The stored callback is refreshed at commit, so a render prop that calls the returned function while
|
|
12
|
+
* rendering (Slate's `renderElement`) runs the previous render's callback; handlers are unaffected.
|
|
13
|
+
*
|
|
11
14
|
* @param callback - The function that should be persisted
|
|
12
15
|
* @returns A stable function reference that will always call the most recent version of the provided callback
|
|
13
16
|
*
|
|
@@ -44,6 +47,20 @@ import { useInsertionEffect, useRef } from 'react';
|
|
|
44
47
|
*
|
|
45
48
|
* return <div>...</div>;
|
|
46
49
|
* };
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* // Render props run during render, so they see the previous render's callback
|
|
53
|
+
* const MyEditor = () => {
|
|
54
|
+
* const [active, setActive] = useState(true);
|
|
55
|
+
*
|
|
56
|
+
* const renderElement = usePersistentFunction((props) => (
|
|
57
|
+
* <Element {...props} onDelete={() => setActive(false)} />
|
|
58
|
+
* ));
|
|
59
|
+
*
|
|
60
|
+
* // Fine here: the callback closes over `setActive`, which React keeps stable.
|
|
61
|
+
* // Reading `active` inside it would give the previous render's value instead.
|
|
62
|
+
* return <Editable renderElement={renderElement} />;
|
|
63
|
+
* };
|
|
47
64
|
*/
|
|
48
65
|
export default function usePersistentFunction(callback) {
|
|
49
66
|
const holderRef = useRef(null);
|