@khanacademy/wonder-blocks-core 11.0.1 → 11.1.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @khanacademy/wonder-blocks-core
2
2
 
3
+ ## 11.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 7516b239: Update useOnMountEffect to pass isMountedRef to callback
8
+
3
9
  ## 11.0.1
4
10
 
5
11
  ### Patch Changes
package/dist/es/index.js CHANGED
@@ -291,7 +291,14 @@ function useLatestRef(value) {
291
291
  }
292
292
 
293
293
  const useOnMountEffect = callback => {
294
- React.useEffect(callback, []);
294
+ const isMountedRef = React.useRef(true);
295
+ React.useEffect(() => {
296
+ const cleanup = callback(isMountedRef);
297
+ return () => {
298
+ cleanup == null ? void 0 : cleanup();
299
+ isMountedRef.current = false;
300
+ };
301
+ }, []);
295
302
  };
296
303
 
297
304
  const useOnline = () => {
@@ -1,9 +1,10 @@
1
+ import * as React from "react";
1
2
  /**
2
3
  * Runs a callback once on mount.
3
4
  *
4
5
  * If the callback returns a cleanup function, it will be called when the component is unmounted.
5
6
  *
6
- * @param {() => (void | (() => void))} callback function that forces the component to update.
7
+ * @param {(isMountedRef: React.MutableRefObject<boolean>) => void | (() => void)} callback function that forces the component to update.
7
8
  *
8
9
  * The following code snippets are equivalent
9
10
  * ```
@@ -25,5 +26,26 @@
25
26
  * }, []);
26
27
  *
27
28
  * If you only need to do something on mount, don't return a cleanup function from `callback`.
29
+ *
30
+ * If your callback is async, use the `isMountedRef` ref that's passed to the callback to ensure
31
+ * that the component using `useOnMountEffect` hasn't been unmounted, e.g.
32
+ *
33
+ * ```
34
+ * const MyComponent = () => {
35
+ * const [foo, setFoo] = React.useState("");
36
+ * useOnMountEffect((isMountedRef) => {
37
+ * const action = async () => {
38
+ * const res = await fetch("/foo");
39
+ * const text = res.text();
40
+ * if (isMountedRef.current) {
41
+ * setFoo(text);
42
+ * }
43
+ * }
44
+ *
45
+ * action();
46
+ * });
47
+ *
48
+ * return foo !== "" ? <h1>Loading...</h1> : <h1>{foo}</h1>;
49
+ * }
28
50
  */
29
- export declare const useOnMountEffect: (callback: () => void | (() => void)) => void;
51
+ export declare const useOnMountEffect: (callback: (isMountedRef: React.MutableRefObject<boolean>) => void | (() => void)) => void;
package/dist/index.js CHANGED
@@ -318,7 +318,14 @@ function useLatestRef(value) {
318
318
  }
319
319
 
320
320
  const useOnMountEffect = callback => {
321
- React__namespace.useEffect(callback, []);
321
+ const isMountedRef = React__namespace.useRef(true);
322
+ React__namespace.useEffect(() => {
323
+ const cleanup = callback(isMountedRef);
324
+ return () => {
325
+ cleanup == null ? void 0 : cleanup();
326
+ isMountedRef.current = false;
327
+ };
328
+ }, []);
322
329
  };
323
330
 
324
331
  const useOnline = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@khanacademy/wonder-blocks-core",
3
- "version": "11.0.1",
3
+ "version": "11.1.0",
4
4
  "design": "v1",
5
5
  "publishConfig": {
6
6
  "access": "public"