https://github.com/kunukn/use-mount
React onMount and onUnMount hook
https://github.com/kunukn/use-mount
Last synced: about 2 months ago
JSON representation
React onMount and onUnMount hook
- Host: GitHub
- URL: https://github.com/kunukn/use-mount
- Owner: kunukn
- License: mit
- Created: 2019-10-30T19:58:39.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T23:47:58.000Z (over 2 years ago)
- Last Synced: 2025-01-31T07:31:10.668Z (4 months ago)
- Language: TypeScript
- Size: 929 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# use-mount
## About
ReactJS Mount and UnMount callback hook.
```jsx
let ref = useMount({ onMount, onUnmount });
/*
onMount is an optional callback function,
onUnmount is an optional callback function.
The ref is attached to the component where the callbacks should apply.
*/
```## Usage example
```jsx
import useMount from '@kunukn/use-mount';let MyComponent = ({ children }) => {
let onMount = node => console.log('I mounted this node in the DOM:', node);
let onUnmount = node => console.log('I unmounted this node from the DOM:', node);
let ref = useMount({ onMount, onUnmount });
return{children};
};let App = () => {
let [isActive, setToggle] = React.useState(false);return (
setToggle(s => !s)}>toggle
{isActive && This is my component}
);
};
```## Demo
https://codesandbox.io/s/gifted-murdock-m3rzk