Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

322
Views
Uncaught Error: 'node' must be a RefObject when passing a React.RefObject

I'm trying to pass in a reference to the exportComponentAsPNG function.

return (
    <div id="grid">
        <div id="pixels" ref={this.gridRef}>{rows}</div>

        {(() => {
            if (shouldExport) {
                exportComponentAsPNG(this.gridRef);
            } else {
                console.log("shouldn't export");
            }
        })()}

        <button onClick={() => exportComponentAsPNG(this.gridRef)} className="mocha-button">Export</button>
    </div>
)

this.gridRef is defined in the constructor as:

constructor(props) {
    super(props);
    this.gridRef = React.createRef();
}

The exportComponentAsPNG(this.gridRef) works with the button component. However, when I try to call it in the if-block it gives me the following error:

Uncaught Error: 'node' must be a RefObject
    at d (index.js:171:1)
    at p (index.js:171:1)
    at Grid.js:38:1
    at Grid.render (Grid.js:42:1)
    at finishClassComponent (react-dom.development.js:20561:1)
    at updateClassComponent (react-dom.development.js:20507:1)
    at beginWork (react-dom.development.js:22440:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:4161:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:4210:1)
    at invokeGuardedCallback (react-dom.development.js:4274:1)

Could you explain why the call from the if-block is different and how to fix it because in my case it would not be ideal to use the button implementation; I would prefer to call it from the if-block.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The girdRef.current is null in the first render. Even if you make it works with null checks, it is not a good practice to use a function like that.

You should use componentDidMount or componentDidUpdate to write your action based on your needs.

componentDidUpdate() {
  if (shouldExport) {
    exportComponentAsPNG(this.gridRef);
  } else {
    console.log("shouldn't export");
  }
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!