@khanacademy/wonder-blocks-core
Advanced tools
Comparing version 0.0.0-PR2414-20241220221259 to 0.0.0-PR2420-20250107230749
# @khanacademy/wonder-blocks-core | ||
## 0.0.0-PR2414-20241220221259 | ||
## 0.0.0-PR2420-20250107230749 | ||
### Minor Changes | ||
- 7516b239: Update useOnMountEffect to pass isMountedRef to callback | ||
### Patch Changes | ||
- 631850f3: Fixes keyboard tests in Dropdown and Clickable | ||
## 11.0.1 | ||
### Patch Changes | ||
- 11a0f5c6: No functional changes. Adding prepublishOnly script. | ||
@@ -8,0 +18,0 @@ |
@@ -294,3 +294,10 @@ import _extends from '@babel/runtime/helpers/extends'; | ||
const useOnMountEffect = callback => { | ||
React.useEffect(callback, []); | ||
const isMountedRef = React.useRef(true); | ||
React.useEffect(() => { | ||
const cleanup = callback(isMountedRef); | ||
return () => { | ||
cleanup == null ? void 0 : cleanup(); | ||
isMountedRef.current = false; | ||
}; | ||
}, []); | ||
}; | ||
@@ -352,2 +359,11 @@ | ||
export { Id, InitialFallback, RenderState, RenderStateRoot, Server, Text, View, addStyle, useForceUpdate, useIsMounted, useLatestRef, useOnMountEffect, useOnline, usePreHydrationEffect, useRenderState }; | ||
const keys = { | ||
enter: "Enter", | ||
escape: "Escape", | ||
tab: "Tab", | ||
space: "Space", | ||
up: "ArrowUp", | ||
down: "ArrowDown" | ||
}; | ||
export { Id, InitialFallback, RenderState, RenderStateRoot, Server, Text, View, addStyle, keys, useForceUpdate, useIsMounted, useLatestRef, useOnMountEffect, useOnline, usePreHydrationEffect, useRenderState }; |
@@ -0,1 +1,2 @@ | ||
import * as React from "react"; | ||
/** | ||
@@ -6,3 +7,3 @@ * Runs a callback once on mount. | ||
* | ||
* @param {() => (void | (() => void))} callback function that forces the component to update. | ||
* @param {(isMountedRef: React.MutableRefObject<boolean>) => void | (() => void)} callback function that forces the component to update. | ||
* | ||
@@ -29,3 +30,24 @@ * The following code snippets are equivalent | ||
* If you only need to do something on mount, don't return a cleanup function from `callback`. | ||
* | ||
* If your callback is async, use the `isMountedRef` ref that's passed to the callback to ensure | ||
* that the component using `useOnMountEffect` hasn't been unmounted, e.g. | ||
* | ||
* ``` | ||
* const MyComponent = () => { | ||
* const [foo, setFoo] = React.useState(""); | ||
* useOnMountEffect((isMountedRef) => { | ||
* const action = async () => { | ||
* const res = await fetch("/foo"); | ||
* const text = res.text(); | ||
* if (isMountedRef.current) { | ||
* setFoo(text); | ||
* } | ||
* } | ||
* | ||
* action(); | ||
* }); | ||
* | ||
* return foo !== "" ? <h1>Loading...</h1> : <h1>{foo}</h1>; | ||
* } | ||
*/ | ||
export declare const useOnMountEffect: (callback: () => void | (() => void)) => void; | ||
export declare const useOnMountEffect: (callback: (isMountedRef: React.MutableRefObject<boolean>) => void | (() => void)) => void; |
@@ -18,1 +18,2 @@ export { default as Text } from "./components/text"; | ||
export type { AriaProps, StyleType, PropsFor } from "./util/types"; | ||
export { keys } from "./util/keys"; |
@@ -321,3 +321,10 @@ 'use strict'; | ||
const useOnMountEffect = callback => { | ||
React__namespace.useEffect(callback, []); | ||
const isMountedRef = React__namespace.useRef(true); | ||
React__namespace.useEffect(() => { | ||
const cleanup = callback(isMountedRef); | ||
return () => { | ||
cleanup == null ? void 0 : cleanup(); | ||
isMountedRef.current = false; | ||
}; | ||
}, []); | ||
}; | ||
@@ -379,2 +386,11 @@ | ||
const keys = { | ||
enter: "Enter", | ||
escape: "Escape", | ||
tab: "Tab", | ||
space: "Space", | ||
up: "ArrowUp", | ||
down: "ArrowDown" | ||
}; | ||
exports.Id = Id; | ||
@@ -388,2 +404,3 @@ exports.InitialFallback = InitialFallback; | ||
exports.addStyle = addStyle; | ||
exports.keys = keys; | ||
exports.useForceUpdate = useForceUpdate; | ||
@@ -390,0 +407,0 @@ exports.useIsMounted = useIsMounted; |
{ | ||
"name": "@khanacademy/wonder-blocks-core", | ||
"version": "0.0.0-PR2414-20241220221259", | ||
"version": "0.0.0-PR2420-20250107230749", | ||
"design": "v1", | ||
@@ -28,3 +28,3 @@ "publishConfig": { | ||
"@khanacademy/wb-dev-build-settings": "^2.0.0", | ||
"@khanacademy/wonder-blocks-testing-core": "0.0.0-PR2414-20241220221259" | ||
"@khanacademy/wonder-blocks-testing-core": "^2.0.1" | ||
}, | ||
@@ -31,0 +31,0 @@ "author": "", |
66763
26
1641