Skip to content

Instantly share code, notes, and snippets.

@ilhamgusti
Created January 10, 2022 03:40
Show Gist options
  • Save ilhamgusti/9bfeca8d88ae89db29b658be0839469a to your computer and use it in GitHub Desktop.
Save ilhamgusti/9bfeca8d88ae89db29b658be0839469a to your computer and use it in GitHub Desktop.
hooks for delay
export function useDelay(time = 1000) {
return React.useCallback(async () => {
return await new Promise((resolve) => {
setTimeout(resolve, time);
});
}, [time]);
}
//usage
export default function App() {
const [isWorking, setWorking] = React.useState(null);
const delayed = useDelay(5000);
delayed().then(() => setWorking(true)); // delay for 5 seconds
return <pre>{JSON.stringify({ isWorking }, null, 2)}</pre>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment