@react-md/utils
Version:
General utils for react-md.
32 lines • 892 B
JavaScript
import { useEffect, useRef } from "react";
/**
* A simple hook that only triggers the callback when a component is unmounted.
* This will make sure that the callback function does not have a stale closure
* by the time the component unmounts as well.
*
* @example
* Simple Example
* ```ts
* useOnUnmount(() => {
* console.log('Component is unmounted.');
* });
*
* const [data, setData] = useState(initialData);
* useOnUnmount(() => {
* API.saveCurrentData(data);
* });
*
* // update data
* ```
*
* @remarks \@since 2.7.1
* @param callback - the function to call when the component unmounts.
*/
export function useOnUnmount(callback) {
var ref = useRef(callback);
useEffect(function () {
ref.current = callback;
});
return useEffect(function () { return function () { return ref.current(); }; }, []);
}
//# sourceMappingURL=useOnUnmount.js.map