Created
April 27, 2020 18:48
-
-
Save sibelius/fc903e55e9982f0b415cbaa5cfc68ddd to your computer and use it in GitHub Desktop.
useTransition polyfill
This file contains 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 { unstable_withSuspenseConfig, useState, useCallback } from 'react' | |
// based on https://github.com/facebook/relay/commit/1befdc085bceef5c78f8eb8c2117459ce4b9d7c7#diff-c8dcd2c1e7d048e3b69a8538434cbca4 | |
function useSuspenseTransition(config: {|timeoutMs: number|}) { | |
const [isPending, setPending] = useState(false); | |
const startTransition = useCallback( | |
(callback: () => void) => { | |
setPending(true); | |
Scheduler.unstable_next(() => { | |
unstable_withSuspenseConfig(() => { | |
setPending(false); | |
callback(); | |
}, config); | |
}); | |
}, | |
[config, setPending], | |
); | |
return [startTransition, isPending]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment