Skip to content

Instantly share code, notes, and snippets.

@tfiechowski
Last active January 9, 2021 00:21
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 tfiechowski/2b70fae728b5e21c7fb1bce6932d7cda to your computer and use it in GitHub Desktop.
Save tfiechowski/2b70fae728b5e21c7fb1bce6932d7cda to your computer and use it in GitHub Desktop.
import { useCallback, useState } from "react";
import { wait } from "./src/FunctionalUpdates/wait";
export function FunctionalUpdates() {
const [counter, setCounter] = useState(0);
function handleIncrement() {
setCounter(counter + 1);
}
const handleIncrementCallback = useCallback(async () => {
await wait({ miliseconds: 2000 });
setCounter(counter + 1);
}, [counter, setCounter]);
return (
<div>
<div>Counter value: {counter}</div>
<button onClick={handleIncrement}>
Increment
</button>
<button onClick={handleIncrementCallback}>
Increment useCallback
</button>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment