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'));

Search Flipkart Products:
Flipkart.com

29 comments:

Unknown said...

Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
Data Science training in chennai
Data science training in velachery
Data science training in tambaram
Data Science training in OMR
Data Science training in anna nagar
Data Science training in chennai
Data science training in Bangalore

simbu said...

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

Unknown said...

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

Unknown said...

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

saimouni said...

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

Mounika said...

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

gowsalya said...

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

Unknown said...

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

Anonymous said...

Impressive. Your story always bring hope and new energy. Keep up the good work.


angularjs Training in chennai
angularjs-Training in pune

angularjs-Training in chennai

angularjs Training in chennai

angularjs-Training in tambaram

angularjs-Training in sholinganallur

Anjali Siva said...

Informative post, thanks for taking time to share this page admin.
ccna Training in Chennai
ccna course in Chennai
RPA Training in Chennai
Robotics Process Automation Training in Chennai
Blockchain Training in Chennai
AWS Certification in Chennai

sathyaramesh said...
This comment has been removed by the author.
sathyaramesh said...

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

Vicky Ram said...

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

myfreearticledirectory
Education

sultana said...

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

mercyroy said...

More informative,thanks for sharing with us.
this blog makes the readers more enjoyable.keep add more info on your page.
Python Training in Nungambakkam
python scripting training in bangalore
python language training in bangalore
python training courses in bangalore

aruna ram said...

Thank you for your nice post. I glad to read your blog. I want to more updates….
SEO Course in Aminjikarai
SEO Training in Vadapalani
SEO Course in Chennai
SEO Classes near me
SEO Training in Karappakkam
SEO Training in Navalur

kayal m said...

Nice content! These concept is very helpful for enhance my knowledge. Thanks for you great blog. I need more sharing from your blog….
Big Data Hadoop Course in Bangalore
Big Data Hadoop Training institutes in Bangalore
Big Data Hadoop Training institute in Bangalore
Big Data Hadoop Training in Vadapalani
Big Data Hadoop Course in Nungambakkam
Big Data Hadoop Course in kandanchavadi
Big Data Hadoop Training in sholinganallur

sathyaramesh said...

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 

Anbarasan14 said...

Your blog is nice. I believe this will surely help the readers who are really in need of this vital piece of information. Thanks for sharing and kindly keep updating.

English Speaking Course in Mulund
Best English Speaking Class in Mulund West
English Speaking Training in Mulund East
English Speaking Course in Mulund
Best English Speaking Classes in Mulund
Best English Speaking Coaching in Mulund East
Spoken English Training Institute near me

LindaJasmine said...

Amazing Article . Thanks for sharing the wonderful Post. Your writing is very interesting to read.
Pega training in chennai
Pega course in chennai
Pega training courses
Pega administrator training
Node JS Training in Chennai
Node JS Course in Chennai
Photoshop Classes in Chennai
Photoshop Course in Chennai

janitha said...

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

Best Training Institute said...

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

kishor said...

nices information thanku so much this information
bluehost-discounts
digital marketing tips

Health Insurance in UAE said...

thanku so much this information
valueking

Aimore Technologies said...

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

Aimore Technologies said...

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

VIPTHANOSS said...

vedanta

SwarnApp said...

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

SwarnApp said...

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