user3226524
user3226524

Reputation: 103

How to use prevState here?

I am getting the value of previous state on change and I get the updated state on next click. I am sure that we have to use prevstate here but I am not getting how.

```````````````````````````````````````````````
 event.currentTarget.name == 'isprivate' ? 
   this.setState({[event.currentTarget.name] : event.currentTarget.value}) 
 : this.setState({[event.target.name] : event.target.value}) 

``````````````````````````````````````````````` 

How do I use prevState here?

Upvotes: 0

Views: 52

Answers (1)

DSCH
DSCH

Reputation: 2396

You can use setState this way:

this.setState((prevState) => prevState.flag? ({ myKey: true}) : ({myKey: false}))

Or do whatever else you want with the prevState available when setting the new state. Hope it helps!

Upvotes: 1

Related Questions