Last active
July 23, 2020 10:18
-
-
Save idrisadetunmbi/14afb8b03ebed596f56ff12e88727772 to your computer and use it in GitHub Desktop.
Hook that runs when dependencies changes and reruns on window resize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useLayoutEffect } from "react" | |
export const useResizeEffect = (effect, deps = []) => { | |
// might need to wrap effect in a useCallback | |
useLayoutEffect(() => { | |
effect() | |
window.addEventListener('resize', effect) | |
return () => window.removeEventListener('resize', effect) | |
}, deps) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment