Skip to content

Instantly share code, notes, and snippets.

@ChaseH88
Created April 5, 2021 04:50
Show Gist options
  • Select an option

  • Save ChaseH88/4eecc30bd12e049daddd7889eabef369 to your computer and use it in GitHub Desktop.

Select an option

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.
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