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

96
Views
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 answers
Answer question

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 Report

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 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!