Skip to content

Instantly share code, notes, and snippets.

@idrisadetunmbi
Last active July 23, 2020 10:18
Show Gist options
  • Save idrisadetunmbi/14afb8b03ebed596f56ff12e88727772 to your computer and use it in GitHub Desktop.
Save idrisadetunmbi/14afb8b03ebed596f56ff12e88727772 to your computer and use it in GitHub Desktop.
Hook that runs when dependencies changes and reruns on window resize
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