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

98
Visualizações
skip re-render using shouldComponentUpdate and nextState

I have currently a drop-down select to filter some charts after 'Apply'. It works fine.(See screenshot below). enter image description here

The problem is that when another timespan gets selected, React does a re-render to all charts before I click 'Apply' button.

I want to avoid this unnecessary re-render by implementingshouldComponentUpdate, but I can't figure out how.

Below what I tried but it did not work(still a re-render):

shouldComponentUpdate(nextState) {
        if (this.state.timespanState !== nextState.timespanState) {
            return true;
        }
        
        return false;
    }

But it always return true, because nextState.timespanState is undefined. Why?

Drop-down Select

<Select value={this.state.timespanState} onChange={this.handleTimeSpanChange}>

handleTimeSpanChange = (event) => {
        this.setState({ timespanState: event.target.value });
    };

constructor(props) {
        super(props);
        this.state = { timespanState: 'Today'};
        this.handleTimeSpanChange = this.handleTimeSpanChange.bind(this);
    }

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You're on the right track with using shouldComponentUpdate, it's just that the first parameter is nextProps and the second is nextState, so in your case, the undefined value is actually nextProps with the wrong name.

Change your code to this,

shouldComponentUpdate(nextProps,nextState) { // <-- tweak this line
        if (this.state.timespanState !== nextState.timespanState) {
            return true;
        }
        
        return false;
    }

about 4 years ago · Juan Pablo Isaza Relatório

0

Finally, I solve the problem by separating drop-down selectbox and charts into two apart components and made the drop-down component as a child component from its parent component, charts components. The reason is the following statement

React components automatically re-render whenever there is a change in their state or props.

Therefore, React will re-render everything in render() method of this component. So keeping them in two separate components will let them re-render without side effect. In my case, any state changes in drop-down or other states in Filter component, will only cause a re-render inside this component. Then passing the updated states to charts component with a callback function.

Something like below:

Child component

export class Filter extends Component {

  handleApplyChanges = () => {
    this.props.renderPieChart(data);
  }
  
  render(){
    return (
      ...
      <Button onClick={this.handleApplyChanges} />
    );
  }
}

Parent component

export class Charts extends Component{
      constructor(props){
        this.state = { dataForPieChart: []};
        this.renderPieChart = this.renderPieChart.bind(this);
      }
      
      renderPieChart = (data) => {
            this.setState({ dataForPieChart: data });
      }
      
      render(){
        return (
          <Filter renderPieChart={this.renderPieChart} />
          <Chart>
            ...data={this.state.dataForPieChart}
          </Chart>
        );
      }
    }

If still any question, disagreement or suggestions, pls let me know:)

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