Created
February 20, 2019 18:42
-
-
Save alobato/f18eec0115a03042c6851a5a75d02acf to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { useState, useEffect } from 'react' | |
| const useDebounce = (value, delay) => { | |
| const [debouncedValue, setDebouncedValue] = useState(value) | |
| useEffect( | |
| () => { | |
| const handler = setTimeout(() => { | |
| setDebouncedValue(value) | |
| }, delay) | |
| return () => { | |
| clearTimeout(handler) | |
| } | |
| }, | |
| [value, delay] | |
| ) | |
| return debouncedValue | |
| } | |
| export default useDebounce |
This file contains hidden or 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 { useState, useEffect } from 'react' | |
| export default function useKeyPress(targetKey) { | |
| // State for keeping track of whether key is pressed | |
| const [keyPressed, setKeyPressed] = useState(false) | |
| let prevKey = '' | |
| // If pressed key is our target key then set to true | |
| function downHandler({ key }) { | |
| // checking keep pressing re-rendering | |
| if (prevKey === targetKey) return | |
| if (key === targetKey) { | |
| setKeyPressed(true) | |
| prevKey = key | |
| } | |
| } | |
| // If released key is our target key then set to false | |
| const upHandler = ({ key }) => { | |
| if (key === targetKey) { | |
| setKeyPressed(false) | |
| prevKey = '' | |
| } | |
| } | |
| // Add event listeners | |
| useEffect(() => { | |
| window.addEventListener('keydown', downHandler) | |
| window.addEventListener('keyup', upHandler) | |
| // Remove event listeners on cleanup | |
| return () => { | |
| window.removeEventListener('keydown', downHandler) | |
| window.removeEventListener('keyup', upHandler) | |
| } | |
| }, []) // Empty array ensures that effect is only run on mount and unmount | |
| return keyPressed | |
| } |
This file contains hidden or 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' | |
| const useLockBodyScroll = () => { | |
| useLayoutEffect(() => { | |
| // Prevent scrolling on mount | |
| document.body.style.overflow = 'hidden'; | |
| // Re-enable scrolling when component unmounts | |
| return () => document.body.style.overflow = 'visible'; | |
| }, []); // Empty array ensures effect is only run on mount and unmount | |
| } | |
| export default useLockBodyScroll |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment