r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

9

u/geoffgiller8 Mar 18 '19

I love this when using react: this.setState({ toggle: !toggle });

34

u/snorkl-the-dolphine Mar 18 '19

This can cause issues of it runs twice in quick succession. The safe way to do this is:

this.setState(({toggle}) => {toggle:!toggle});

16

u/[deleted] Mar 19 '19

this && setState? What is this, 2018? 😄

4

u/beforan Mar 19 '19

Oh shit, I guess I have to do no work today while I read about hooks and refactor some components

→ More replies (3)