DEV Community

Chris
Chris

Posted on

1

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay