-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: Jonatan Asketorp <k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com>
- Loading branch information
1 parent
c7b5d88
commit 693562e
Showing
3 changed files
with
82 additions
and
28 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useEffect } from 'react'; | ||
|
||
export function useOnResize(ref, handler) { | ||
useEffect( | ||
() => { | ||
const listener = () => { | ||
handler(); | ||
}; | ||
|
||
window.addEventListener('resize', listener); | ||
|
||
return () => { | ||
window.removeEventListener('resize', listener); | ||
}; | ||
}, | ||
// Add ref and handler to effect dependencies | ||
// It's worth noting that because passed in handler is a new ... | ||
// ... function on every render that will cause this effect ... | ||
// ... callback/cleanup to run every render. It's not a big deal ... | ||
// ... but to optimize you can wrap handler in useCallback before ... | ||
// ... passing it into this hook. | ||
[ref, handler], | ||
); | ||
} |
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