Skip to content

Instantly share code, notes, and snippets.

@Landerson352
Last active August 21, 2020 16:38
Show Gist options
  • Save Landerson352/afd4f6f01907b7d5104cb7e44ab4126d to your computer and use it in GitHub Desktop.
Save Landerson352/afd4f6f01907b7d5104cb7e44ab4126d to your computer and use it in GitHub Desktop.
A "componentWillUnmount" hook with dependencies.
import React from 'react';
// Usage: useOnUnmount((deps) => console.log(deps), deps);
const useOnUnmount = (fn, deps) => {
const ref = React.useRef(text);
React.useEffect(() => {
ref.current = deps;
}, [deps]);
React.useEffect(() => {
return () => fn(ref.current);
}, []);
};
export default useOnUnmount;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment