DEV Community

Chris
Chris

Posted on

Answer: Event Handler Not Setting State Before Component Renders

You should not setState within a setState callback function. Instead it should return the new state. This:

onHandlePrint = (pdf) => {
    this.setState({pdf}, () => {
      this.setState({pdfStatus: true})
    });
  };

should be:

onHandlePrint = (pdf) => {
    this.setState(() => {pdf, pdfStatus: true});
  };

But really if you don't need to…

Top comments (0)