SlideShare a Scribd company logo
React application lifecycle
1 / 31
2 / 31
React + JSX
<Modal>
<ModalHeader>
<ModalTitle>Modaltitle</ModalTitle>
</ModalHeader>
<ModalBody>
Onefinebody...
</ModalBody>
<ModalFooter>
<Button>Close</Button>
<Buttontype="primary">Savechanges</Button>
</ModalFooter>
</Modal>
3 / 31
HTML
<divclass="modalfade">
<divclass="modal-dialog">
<divclass="modal-content">
<divclass="modal-header">
<buttontype="button"class="close"data-dismiss="modal"aria-hidden="true"
<h4class="modal-title">Названиемодали</h4>
</div>
<divclass="modal-body">
<p>Onefinebody…</p>
</div>
<divclass="modal-footer">
<buttontype="button"class="btnbtn-default"data-dismiss="modal">Закрыть</
<buttontype="button"class="btnbtn-primary">Сохранитьизменения</button>
</div>
</div><!--/.modal-content-->
</div><!--/.modal-dialog-->
</div><!--/.modal-->
4 / 31
Birth / Mounting
1. Initialize / Construction
5 / 31
Birth / Mounting
1. Initialize / Construction
2. getDefaultProps()(React.createClass) or MyComponent.defaultProps(ES6 class)
6 / 31
Component props
importCowboyfrom'react';
exportdefaultclassPersonextendsReact.Component{
render(){
return(
<div>{this.props.name}(weapon:{this.props.weapon})</div>
);
}
}
<Personname="ClintEastwood"weapon="Colt45"/>
<div>ClintEastwood(weapon:Colt45)</div>
7 / 31
getDefaultProps()
importReactfrom'react';
classCowboyextendsReact.Component{
render(){
return(
<div>{this.props.name}(weapon:{this.props.weapon})</div>
);
}
}
Person.defaultProps={name:'Gringo',weapon:'unknown'};
<Personname="ClintEastwood"/>
<div>ClintEastwood(weapon:unknown)</div>
8 / 31
For react null is value
<Personname="ClintEastwood"weapon={null}/>
<div>ClintEastwood(weapon:)</div>
9 / 31
Birth / Mounting
1. Initialize / Construction
2. getDefaultProps()(React.createClass) or MyComponent.defaultProps(ES6 class)
3. getInitialState()(React.createClass) or this.state=...(ES6 constructor)
10 / 31
React state
importReactfrom'react';
classCowboyextendsReact.Component{
constructor(props){
super(props);
this.state={isDead:false};
}
kill(){
this.setState({isDead:true});
}
render(){
return(
<div>
{this.state.isDead?
"(RIP)":
<buttononClick={()=>{this.kill()}}>Kill</button>}
{this.props.name}(weapon:{this.props.weapon})</div>
);
}
}
Person.defaultProps={name:'Gringo',weapon:'unknown'};
React state 11 / 31
Birth / Mounting
1. Initialize / Construction
2. getDefaultProps()(React.createClass) or MyComponent.defaultProps(ES6 class)
3. getInitialState()(React.createClass) or this.state=...(ES6 constructor)
4. componentWillMount()
5. render()
12 / 31
render()
importReactfrom'react';
classCowboyextendsReact.Component{
constructor(props){
super(props);
this.state={isDead:false};
}
kill(){
this.setState({isDead:true});
this.state.isDead;//false
}
render(){
return(
<div>
{this.state.isDead?
"(RIP)":
<buttononClick={()=>{this.kill()}}>Kill</button>}
{this.props.name}(weapon:{this.props.weapon})
</div>
);
}
}
Person.defaultProps={name:'Gringo',weapon:'unknown'};
Context problem
13 / 31
Resolve context problem
1. Bind functions in constructor
constructor(props){
super(props);
this.state={isDead:false};
this.kill=this.kill.bind(this);
}
2. Pass binded functions
<buttononClick={this.kill.bind(this)}>Kill</button>
3. Use autobind decorator on function decoration
@autobind
kill(){
this.setState({isDead:true});
this.state.isDead;//false
}
14 / 31
Birth / Mounting
1. Initialize / Construction
2. getDefaultProps()(React.createClass) or MyComponent.defaultProps(ES6 class)
3. getInitialState()(React.createClass) or this.state=...(ES6 constructor)
4. componentWillMount()
5. render()
6. Children initialization & life cycle kickoff
15 / 31
Birth / Mounting
1. Initialize / Construction
2. getDefaultProps()(React.createClass) or MyComponent.defaultProps(ES6 class)
3. getInitialState()(React.createClass) or this.state=...(ES6 constructor)
4. componentWillMount()
5. render()
6. Children initialization & life cycle kickoff
7. componentDidMount()
16 / 31
17 / 31
Growth / Update
1. componentWillReceiveProps()
18 / 31
Growth / Update
1. componentWillReceiveProps()
2. shouldComponentUpdate()
19 / 31
Growth / Update
1. componentWillReceiveProps()
2. shouldComponentUpdate()
20 / 31
Growth / Update
1. componentWillReceiveProps()
2. shouldComponentUpdate()
3. componentWillUpdate()
21 / 31
Component will update every time when
state or props changed
Default REACT implementation of componentWillUpdate:
componentWillUpdate(newProps,nextState){
constcurrentProps=this.props;
constcurrentState=this.state;
returntrue;
}
22 / 31
Growth / Update
1. componentWillReceiveProps()
2. shouldComponentUpdate()
3. componentWillUpdate()
4. render()(rerender)
23 / 31
Growth / Update
1. componentWillReceiveProps()
2. shouldComponentUpdate()
3. componentWillUpdate()
4. render()(rerender)
5. Children Life cycle methods
24 / 31
25 / 31
Growth / Update
1. componentWillReceiveProps()
2. shouldComponentUpdate()
3. componentWillUpdate()
4. render()(rerender)
5. Children Life cycle methods
6. componentDidUpdate()
26 / 31
Death / Unmount
1. componentWillUnmount()
2. Children Life cycle methods
3. Instance destroyed by Garbage Collector
27 / 31
JSX is not HTML
JSX code
<div>
<h4>Header</h4>
<button>pessme</button>
</div>
transforms to JS code
React.createElement(
"div",
null,
React.createElement(
"h4",
null,
"Header"
),
React.createElement(
"button",
null,
"pessme"
)
);
}
}]); 28 / 31
Test react using Jest
//Link.react-test.js
importReactfrom'react';
importLinkfrom'../Link.react';
importrendererfrom'react-test-renderer';
test('Linkchangestheclasswhenhovered',()=>{
constcomponent=renderer.create(
<Linkpage="http://www.facebook.com">Facebook</Link>
);
lettree=component.toJSON();
expect(tree).toMatchSnapshot();
//manuallytriggerthecallback
tree.props.onMouseEnter();
//re-rendering
tree=component.toJSON();
expect(tree).toMatchSnapshot();
//manuallytriggerthecallback
tree.props.onMouseLeave();
//re-rendering
tree=component.toJSON();
expect(tree).toMatchSnapshot();
});
29 / 31
Jest creates shapshots
/__tests__/__snapshots__/Link.react-test.js.snap
exports[`Linkchangestheclasswhenhovered1`]=`
<a
className="normal"
href="http://www.facebook.com"
onMouseEnter={[Function]}
onMouseLeave={[Function]}>
Facebook
</a>
`;
exports[`Linkchangestheclasswhenhovered2`]=`
<a
className="hovered"
href="http://www.facebook.com"
onMouseEnter={[Function]}
onMouseLeave={[Function]}>
Facebook
</a>
`;
exports[`Linkchangestheclasswhenhovered3`]=`
<a
className="normal"
href="http://www.facebook.com"
onMouseEnter={[Function]}
onMouseLeave={[Function]}>
Facebook
</a>
30 / 31
31 / 31

More Related Content

PPTX
Odoo Website - How to Develop Building Blocks
PDF
Mvvmintroduction 130725124207-phpapp01
PPTX
Integrating With The Windows 8 Experiences
PDF
CSS-в-JS, HTML-в-JS, ВСЁ-в-JS. Все гораздо проще, когда всё вокруг JavaScript
PDF
QCon 2015 - Thinking in components: A new paradigm for Web UI
PDF
Stole16
PDF
Building beautiful websites with bootstrap a case study (DevelopMentor webcast)
DOCX
Kohana bootstrap - modal form
Odoo Website - How to Develop Building Blocks
Mvvmintroduction 130725124207-phpapp01
Integrating With The Windows 8 Experiences
CSS-в-JS, HTML-в-JS, ВСЁ-в-JS. Все гораздо проще, когда всё вокруг JavaScript
QCon 2015 - Thinking in components: A new paradigm for Web UI
Stole16
Building beautiful websites with bootstrap a case study (DevelopMentor webcast)
Kohana bootstrap - modal form

Similar to Жизненный цикл React приложений (20)

PDF
Kohana bootstrap - modal form
PDF
Polymer
PDF
HTML Summary Cardsvdgvdfgdgdfgdfgfgdfgdff.pdf
PPTX
MVC and Razor - Doc. v1.2
PDF
Index of jquery template 2 Minuteman Summer Web Dev.
PDF
Jinja2 Templates - San Francisco Flask Meetup
PDF
Understanding form helpers
KEY
Knockout.js presentation
PPTX
Template.engine.concept2012
PPTX
jQuery Mobile - Desenvolvimento para dispositivos móveis
PPTX
JQuery Mobile UI
TXT
Facebook.html
TXT
Facebook.html
TXT
zynga-online.facebook.html
PPTX
Javascript 2
PDF
Bootstrap day4
PDF
Intro to Jinja2 Templates - San Francisco Flask Meetup
ODP
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
PDF
Angular js quickstart
PDF
Alfredo-PUMEX
Kohana bootstrap - modal form
Polymer
HTML Summary Cardsvdgvdfgdgdfgdfgfgdfgdff.pdf
MVC and Razor - Doc. v1.2
Index of jquery template 2 Minuteman Summer Web Dev.
Jinja2 Templates - San Francisco Flask Meetup
Understanding form helpers
Knockout.js presentation
Template.engine.concept2012
jQuery Mobile - Desenvolvimento para dispositivos móveis
JQuery Mobile UI
Facebook.html
Facebook.html
zynga-online.facebook.html
Javascript 2
Bootstrap day4
Intro to Jinja2 Templates - San Francisco Flask Meetup
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
Angular js quickstart
Alfredo-PUMEX
Ad

More from GDG Odessa (9)

PDF
Rx android
PPTX
Kotlin
PPTX
Clean architecture on Android
PPTX
BDSM or start programming after Angular one
PPTX
Angular 2 vs Angular 1
PPTX
Release Engineering
PPTX
Angular 1.x in action now
PDF
Понимая Git /git/. Git изнутри наружу
PDF
React + Redux. Опыт использования
Rx android
Kotlin
Clean architecture on Android
BDSM or start programming after Angular one
Angular 2 vs Angular 1
Release Engineering
Angular 1.x in action now
Понимая Git /git/. Git изнутри наружу
React + Redux. Опыт использования
Ad

Recently uploaded (20)

PPTX
Cloud computing and distributed systems.
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Approach and Philosophy of On baking technology
PPTX
Big Data Technologies - Introduction.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Encapsulation theory and applications.pdf
Cloud computing and distributed systems.
MIND Revenue Release Quarter 2 2025 Press Release
Chapter 3 Spatial Domain Image Processing.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
The AUB Centre for AI in Media Proposal.docx
Approach and Philosophy of On baking technology
Big Data Technologies - Introduction.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Diabetes mellitus diagnosis method based random forest with bat algorithm
NewMind AI Weekly Chronicles - August'25-Week II
Building Integrated photovoltaic BIPV_UPV.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Reach Out and Touch Someone: Haptics and Empathic Computing
gpt5_lecture_notes_comprehensive_20250812015547.pdf
A Presentation on Artificial Intelligence
Mobile App Security Testing_ A Comprehensive Guide.pdf
Machine learning based COVID-19 study performance prediction
Unlocking AI with Model Context Protocol (MCP)
Encapsulation_ Review paper, used for researhc scholars
Encapsulation theory and applications.pdf

Жизненный цикл React приложений