use-good-hooks 1.0.42 → 1.0.43

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/README.md CHANGED
@@ -608,6 +608,28 @@ const DelayedComponent = () => {
608
608
  - `setLate`: A function to update the state after the specified delay. It can also accept a second boolean argument to update the state immediately.
609
609
  - `cancel`: A function to cancel a pending state update.
610
610
 
611
+ ### `useUnmount`
612
+
613
+ Runs a callback when the component unmounts for real. React's Strict Mode simulates an unmount right after mount (cleanup, then setup again on the same instance); a plain `useEffect` cleanup fires there too, which aborts in-flight requests or tears down subscriptions that the remount never rebuilds. `useUnmount` defers the callback by one microtask and cancels it if the effect is set up again, so only the real unmount reaches it. The latest callback is always the one called.
614
+
615
+ ```typescript
616
+ import useUnmount from 'use-good-hooks/use-unmount';
617
+
618
+ const Subscriber = ({ channel }) => {
619
+ const controller = useRef(new AbortController());
620
+
621
+ useUnmount(() => {
622
+ controller.current.abort();
623
+ });
624
+
625
+ return <p>Listening to {channel}</p>;
626
+ };
627
+ ```
628
+
629
+ #### Parameters
630
+
631
+ - `fn`: The callback to run on unmount. It is called once, one microtask after the real unmount, and never on a Strict Mode remount.
632
+
611
633
  ## 🧪 Running Tests
612
634
 
613
635
  This library is thoroughly tested with Vitest and React Testing Library. To run the tests:
@@ -0,0 +1,4 @@
1
+ declare const useUnmount: (fn: () => void) => void;
2
+ export default useUnmount;
3
+
4
+ export { }
@@ -0,0 +1,14 @@
1
+ import { useRef as u, useEffect as n } from "react";
2
+ const s = (e) => {
3
+ const t = u(e), r = u(!1);
4
+ n(() => {
5
+ t.current = e;
6
+ }, [e]), n(() => (r.current = !1, () => {
7
+ r.current = !0, queueMicrotask(() => {
8
+ r.current && t.current();
9
+ });
10
+ }), []);
11
+ };
12
+ export {
13
+ s as default
14
+ };
package/package.json CHANGED
@@ -50,5 +50,5 @@
50
50
  "test:coverage": "vitest --coverage --run",
51
51
  "test:watch": "vitest"
52
52
  },
53
- "version": "1.0.42"
53
+ "version": "1.0.43"
54
54
  }