Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

324
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda