Search code examples
reactjsdestructuring

How can I write those setStates with destructuring on one string?


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.


Solution

  • I think you need to do something like this

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