Skip to content

Instantly share code, notes, and snippets.

@estrattonbailey
Created February 26, 2020 23:18
Show Gist options
  • Save estrattonbailey/5aa4aea59934ad8169d22c24ac5ba227 to your computer and use it in GitHub Desktop.
Save estrattonbailey/5aa4aea59934ad8169d22c24ac5ba227 to your computer and use it in GitHub Desktop.
Delays updating of values.
function Lag<T>({
value,
ttl,
children,
}: {
value: T;
ttl: number;
children(value: T): any;
}) {
const [cache, setCache] = React.useState(value);
React.useEffect(() => {
setTimeout(() => {
setCache(value);
}, ttl);
}, [value]);
return children(cache);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment