Skip to content

Instantly share code, notes, and snippets.

@make-github-pseudonymous-again
Created February 7, 2023 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save make-github-pseudonymous-again/6cc1643900f021ae11dd0baabd287263 to your computer and use it in GitHub Desktop.
Save make-github-pseudonymous-again/6cc1643900f021ae11dd0baabd287263 to your computer and use it in GitHub Desktop.
Can `useDeferredValue` be built on top of `useTransition`?
import {useEffect, useState, useTransition} from 'react';
export const useDeferredValue = (value) => {
const [, startTransition] = useTransition();
const [deferredValue, setDeferredValue] = useState(value);
useEffect(() => {
startTransition(() => {
setDeferredValue(value);
});
}, [value])
return deferredValue;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment