Home > Back-end >  How can I write those setStates with destructuring on one string?
How can I write those setStates with destructuring on one string?

Time:07-26

React
this.setState(({ images }) => ({ images: [...images, ...hits] }));
this.setState({ status: 'resolved', loading: false, totalHits });
I'm trying to shorten those 2 setStates in one but I don't really understand how to do this, I tried to do it like
this.setState(({ images }) => ({ images: [...images, ...hits] }), {status: 'resolved', loading: false, totalHits }); but other states after images become callback function.

CodePudding user response:

I think you need to do something like this

this.setState(({ images }) => ({ images: [...images, ...hits], status: 'resolved', loading: false, totalHits });

CodePudding user response:

Did you try this :

this.setState({
 images: [...this.state.images, ...hits],
 status: 'resolved',
 loading: false,
 totalHits
});
  • Related