Skip to content

Commit 011b577

Browse files
committed
fix: add before/after example to useDisposableMemo jsdoc
1 parent c8207b9 commit 011b577

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/hooks/useDisposableMemo.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,30 @@ function depsEqual(a: DependencyList, b: DependencyList): boolean {
1919
* - On unmount in production: cleaned up synchronously in effect cleanup.
2020
* - On unmount in development: cleanup is deferred via `setTimeout(0)` so that
2121
* fast refresh and Strict Mode can cancel it when effects re-run.
22+
*
23+
* Replaces the common `useMemo` + dispose-in-`useEffect`-cleanup pattern that
24+
* breaks on fast refresh (HMR re-runs all effect cleanups, disposing the native
25+
* object, but `useMemo` returns the same dead reference):
26+
*
27+
* ```tsx
28+
* // BEFORE — breaks on fast refresh
29+
* const property = useMemo(() => instance?.getProperty(path), [instance, path]);
30+
* useEffect(() => {
31+
* const unsub = property?.addListener(setValue);
32+
* return () => { unsub?.(); property?.dispose(); };
33+
* }, [property]);
34+
*
35+
* // AFTER
36+
* const property = useDisposableMemo(
37+
* () => instance?.getProperty(path),
38+
* (p) => p?.dispose(),
39+
* [instance, path]
40+
* );
41+
* useEffect(() => {
42+
* const unsub = property?.addListener(setValue);
43+
* return () => unsub?.(); // only unsubscribe, no dispose
44+
* }, [property]);
45+
* ```
2246
*/
2347
export function useDisposableMemo<T>(
2448
factory: () => T,

0 commit comments

Comments
 (0)