Wednesday, August 15, 2018

shouldComponentUpdate example

The shouldComponentUpdate life-cycle method is invoked in the update phase before the render(). This method will be invoked when the component receives new state or props, this method can be used to prevent re-rendering of the component. If shouldComponentUpdate returns false then render() will not be triggered. By default render() will be called for any state change, this method can be used to override the default behavior and prevent re-rendering of the component.

The following example uses shouldComponentUpdate() to check the value of the state "counter", compares the previous value of the state and the current value of the state in the shouldComponentUpdate method and return true only when the state value is changed, this will trigger render(). If the state value of counter did not change it returns false and render() will not be called. We can check if render is called by looking at the console, since 'Render called.' is written to the console whenever render() is triggered.

import React, {Component} from 'react';
import ReactDOM from 'react-dom';

class HelloShouldComponentUpdate extends Component {
  constructor(props) {
super(props);
this.state = {
counter: 0,
unUsedState: 0
};
this.onIncrementClick = this.onIncrementClick.bind(this);
this.onNoopClick = this.onNoopClick.bind(this);
}

onNoopClick() { 
this.setState({unUsedState: Math.random()});
}

onIncrementClick() { 
this.setState({counter: this.state.counter + 1});
}

shouldComponentUpdate(nextProps, nextState) {
if (nextState.counter !== this.state.counter)
return true;
else
return false;
}

render() {
console.log('Render called.');
return (
      <div>
        <div>Count: {this.state.counter}</div>
        <div><button onClick={this.onIncrementClick}>Increment</button>
        <button onClick={this.onNoopClick}>No-op</button></div>
      </div>
);
}
}

ReactDOM.render(<HelloShouldComponentUpdate />, document.getElementById('app'));

29 comments:

  1. Have you been thinking about the power sources and the tiles whom use blocks I wanted to thank you for this great read!! I definitely enjoyed every little bit of it and I have you bookmarked to check out the new stuff you post
    java training in chennai | java training in bangalore

    java training in tambaram | java training in velachery

    java training in omr | oracle training in chennai

    ReplyDelete
  2. This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolites festivity to pity. I appreciated what you ok extremely here 
    Data Science training in Chennai | Data science training in bangalore
    Data science training in pune| Data science online training
    Pyton training in Kalyan nagar

    ReplyDelete
  3. This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
    java training in chennai

    java training in marathahalli | java training in btm layout

    ReplyDelete
  4. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    python training Course in chennai
    python training in Bangalore
    Python training institute in kalyan nagar

    ReplyDelete
  5. I would like to thank you for your nicely written post, its informative and your writing style encouraged me to read it till end. Thanks
    python training in pune
    python training institute in chennai
    python training in Bangalore

    ReplyDelete
  6. Great content thanks for sharing this informative blog which provided me technical information keep posting.
    Devops Training courses

    Devops Training in Bangalore

    Devops Training in pune

    ReplyDelete
  7. This is very good content you share on this blog. it's very informative and provide me future related information.
    Best Devops Training in pune
    Devops Training in Chennai

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. You have provided a nice article, Thank you very much for this one. And I hope this will be useful for many people. And I am waiting for your next post keep on updating these kinds of knowledgeable things
    Selenium Training in Chennai
    selenium Classes in chennai
    iOS Training in Chennai
    Digital Marketing Training in Chennai
    .Net coaching centre in chennai
    JAVA Training in Chennai
    JAVA Course in Chennai
    Best JAVA Training in Chennai

    ReplyDelete
  10. Nice to see your blog post. Thanks a lot for updating this unique content with us.

    myfreearticledirectory
    Education

    ReplyDelete
  11. I am really enjoying reading your well written articles.
    It looks like you spend a lot of effort and time on your blog.
    I have bookmarked it and I am looking forward to reading new articles. Keep up the good work..
    Java Training in Bangalore
    big data courses in bangalore
    hadoop training institutes in bangalore
    Java Course in Bangalore
    Java Training Institutes in Bangalore
    Java Institutes in Bangalore

    ReplyDelete
  12. This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
    Python Training near me
    Python Training in Chennai
    Python course in Chennai
    Ethical Hacking Course in Chennai 
    Hacking Course in Chennai 
    Ethical Hacking Training in Chennai 

    ReplyDelete
  13. Hey There. I found your blog using msn. This is a very well written article. I’ll be sure to bookmark it and come back to read more of your useful info. Thanks for the post. I’ll definitely return.
    data analytics course malaysia

    ReplyDelete
  14. Hey there it awesome to read and explore your blog my friend. Keep creating like this! Thanks for sharing!
    Sharepoint training in bangalore

    ReplyDelete
  15. Aimore Technologies is the best AI training institute in Chennai with 6+ years of experience. We are offering online and classroom training. Visit Us: Artificial Intelligence Training in Chennai

    ReplyDelete
  16. Aimore Technologies is the best Angularjs training institute in Chennai with 6+ years of experience. We are offering online and classroom training. Visit Us: Angularjs Training in Chennai

    ReplyDelete
  17. Thank you for sharing this valuable information with us.
    jewellery erp software
    Jewellery erp software

    ReplyDelete
  18. Thank you for sharing this valuable information with us.
    Jewellery ERP Software UAE
    Jewellery ERP Software UAE

    ReplyDelete