Skip to content

Instantly share code, notes, and snippets.

@dance2die
Last active January 5, 2022 08:49
Show Gist options
  • Save dance2die/35416648292313d931c7bd6efb930fb8 to your computer and use it in GitHub Desktop.
Save dance2die/35416648292313d931c7bd6efb930fb8 to your computer and use it in GitHub Desktop.
function App() {
const [count, setCount] = useState(0);
const [message, setMessage] = useState("");
function increment() {
setCount(count + 1);
setMessage(`count is ${count}`);
}
function decrement() {
setCount(count - 1);
setMessage(`count is ${count}`);
}
return (
<div className="App">
...
</div>
);
}
@tfiechowski
Copy link

tfiechowski commented Aug 20, 2020

Hey! I encountered this article and started to wonder why haven't you followed Dan's suggestion to calculate next value and update both:

  function increment() {
    const newCount = count + 1;
    setCount(newCount);
    setMessage(`count is ${newCount}`);
  }
  function decrement() {
    const newCount = count - 1;
    setCount(newCount);
    setMessage(`count is ${newCount}`);
  }

What's the reason for not doing so?

@dance2die
Copy link
Author

Thank you, @tfiechowski for the correction~
I don't have an excuse as I misunderstood Dan's suggestion πŸ˜“

I've updated the post and added your gist πŸ˜„

@tfiechowski
Copy link

tfiechowski commented Aug 21, 2020

Sure, no worries!

I'm happy I could contribute and also big appreciation for a mention in the article! πŸ™‡

@dance2die
Copy link
Author

βœ‹ high-five for the contribution~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment