Created
January 10, 2022 03:40
-
-
Save ilhamgusti/9bfeca8d88ae89db29b658be0839469a to your computer and use it in GitHub Desktop.
hooks for delay
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
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