Skip to content

Instantly share code, notes, and snippets.

@dance2die

dance2die/App-bad.js

Last active Aug 21, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@tfiechowski 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

This comment has been minimized.

Copy link
Owner Author

@dance2die dance2die commented Aug 20, 2020

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

This comment has been minimized.

Copy link

@tfiechowski 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

This comment has been minimized.

Copy link
Owner Author

@dance2die dance2die commented Aug 21, 2020

high-five for the contribution~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.