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

402
Visualizações
ReactJS - render called, but DOM not updated

This has happened to me a few times. I have always managed to work around the issue, yet I am still intrigued to understand why this happens, and what I have missed.

Essentially if I have a condition within my render method which specifies the class for my div:

let divClass = this.state.renderCondition ? 'red' : 'blue';

By default I set renderCondition within my state to false.

If I then define an onClick handler on a button (as follows), and click the button, whilst render IS called, the DOM is NOT updated. That is to say the class does not change.

onClickCompile: function() {

   this.setState({renderCondition: true}, function() {

        synchronousSlowFunction();
   });
}

This seems to have something to do with running slow synchronous code in that if the code is quick and simple the DOM IS updated appropriately.

If I wrap the call to synchronousSlowFunction in a 500 millisecond timeout, everything works as expected. I would however like to understand what I have misunderstood such that I do not need this hack.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Can you share the button onClick code? I might be wrong but this looks like an incorrectly set button onClick listener.

Make sure that onClick callback is defined without (), i.e.

<button onClick={this.something} />

instead of:

<button onClick={this.something()} />

Post more code so we can get a better (bigger) picture

over 4 years ago · Santiago Trujillo Relatório

0

synchronousSlowFunction is as you mentioned synchronous. This means, that it blocks your component while it is running. That means, that react cannot update your component, because it has to wait for the callback function to complete until it can call render with the updated values. The setTimeout wrap makes the call asynchronous, so that react can render/update your component, while the function is doing its work. It is not the time delay, that makes it work but simply the callback, which is not render blocking. You could also wrap in in a Promise or make the slow function async to prevent render blocking.

over 4 years ago · Santiago Trujillo Relatório

0

Try something Like this:

this.setState((state) => ({
   ...state, renderCondition: true
}));

Maybe you're doing another setState for renderCondition some where the code above should fix such things.

or maybe try using PureComponent

import React, { PureComponent } from 'react'

export default class TablePreferencesModal extends PureComponent {
  render() {
    return (
      <div>

      </div>
    )
  }
}
over 4 years ago · Santiago Trujillo 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
Assistente virtual