Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created April 27, 2020 18:48
Show Gist options
  • Save sibelius/fc903e55e9982f0b415cbaa5cfc68ddd to your computer and use it in GitHub Desktop.
Save sibelius/fc903e55e9982f0b415cbaa5cfc68ddd to your computer and use it in GitHub Desktop.
useTransition polyfill
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