Created
April 5, 2021 04:50
-
-
Save ChaseH88/4eecc30bd12e049daddd7889eabef369 to your computer and use it in GitHub Desktop.
A hook that gives you a boolean that you can use to delay rendering of your component.
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 { useEffect, useState } from 'react'; | |
| /** | |
| * Adds a render delay variable that can be used on components | |
| * @param timeout | |
| * @param dependency | |
| * @example const visible = useRenderDelay(open, 2500); | |
| */ | |
| const useRenderDelay = (dependency: boolean, timeout = 1000): boolean => { | |
| const [visible, setVisible] = useState(false) | |
| useEffect(() => { | |
| if (dependency) { | |
| setTimeout(() => setVisible(true), timeout); | |
| } | |
| return () => { | |
| setVisible(false) | |
| } | |
| }, [dependency]); | |
| return visible; | |
| } | |
| export { useRenderDelay } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment