Skip to content

Commit

Permalink
feat: add useUnmount hook
Browse files Browse the repository at this point in the history
  • Loading branch information
RiadhAdrani committed Aug 14, 2024
1 parent 5627e6f commit ede6ecf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## Unreleased

### Added

- add `useUnmount` hook that let you execute a callback when a component is unmounted.

## 0.5.9 - 2024-03-31

### Fixed
Expand Down
20 changes: 20 additions & 0 deletions lib/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,26 @@ export const useEffect = (callback: Effect, deps?: unknown): void => {
}
};

/**
* let you execute an callback when the component is unmounted.
* @param callback the function containing your logic.
* @param deps (optional) dependency according to which the callback will be updated. `undefined` by default.
* @example
* ```
* const filtered = useUnmount(() => {
* unsubscribe();
* });
* ```
* @since v0.5.10
*/
export const useUnmount = (callback: () => void): void => {
const fn = useRef(callback);

fn.value = callback;

useEffect(() => () => fn.value?.());
};

/**
* let you cache a computation between re-renders.
* @param callback the function that will compute the value you want to cache, it should not take arguments.
Expand Down

0 comments on commit ede6ecf

Please sign in to comment.