DEV Community 👩‍💻👨‍💻

Discussion on: What a React developer needs to know in 2021

Collapse
 
pcockerell profile image
Peter Cockerell

One of the reasons your class-based component was more complex than the functional one was that you unnecessarily used the function-argument form of setState(). You could have just used

onIncrement = () => this.setState(
{counter: this.state.counter + 1}
);

instead.

Collapse
 
virtualghostmck profile image
virtualghostmck

This could be buggy in some cases.

Collapse
 
pcockerell profile image
Peter Cockerell

How so?

Thread Thread
 
vberen profile image
Nicklas Wessel

Because of the async nature.
If you spam the button it might only increment it by one, since the state at that point at the second click isn't updated yet.

Thread Thread
 
pcockerell profile image
Peter Cockerell

Ah, got it, thanks!