Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda