I mean, technically you're still "setting state", but it won't be called setState when using hooks. You're typically meant to call useState multiple times per each individual piece of state you track, rather than attempting to modify one big state object.
For example, a component that keeps track of an internal count and whether or not to show/hide a popup would use the state hook like:
Careful with syntax! You need a set of parens around the object:
this.setState(({toggle}) => ({toggle: !toggle}))
Even worse, your version won't throw a syntax error because toggle: is interpreted as a label, making it a pretty insidious bug (React should hopefully catch it though).
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});