forked from colinhartigan/valorant-inventory-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix layout for chroma/level selectors
- Loading branch information
1 parent
0e3f76f
commit 2c8df0b
Showing
2 changed files
with
48 additions
and
15 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,32 @@ | ||
import { useState, useRef, useEffect, useCallback } from "react"; | ||
|
||
function useEventListener(eventName, handler, element = window) { | ||
// Create a ref that stores handler | ||
const savedHandler = useRef(); | ||
// Update ref.current value if handler changes. | ||
// This allows our effect below to always get latest handler ... | ||
// ... without us needing to pass it in effect deps array ... | ||
// ... and potentially cause effect to re-run every render. | ||
useEffect(() => { | ||
savedHandler.current = handler; | ||
}, [handler]); | ||
useEffect( | ||
() => { | ||
// Make sure element supports addEventListener | ||
// On | ||
const isSupported = element && element.addEventListener; | ||
if (!isSupported) return; | ||
// Create event listener that calls handler function stored in ref | ||
const eventListener = (event) => savedHandler.current(event); | ||
// Add event listener | ||
element.addEventListener(eventName, eventListener); | ||
// Remove event listener on cleanup | ||
return () => { | ||
element.removeEventListener(eventName, eventListener); | ||
}; | ||
}, | ||
[eventName, element] // Re-run if eventName or element changes | ||
); | ||
} | ||
|
||
export default useEventListener |