Skip to content

Commit e5bb626

Browse files
committed
React - Component Lifecycle Mounting
1 parent 62106b9 commit e5bb626

File tree

6 files changed

+141
-22
lines changed

6 files changed

+141
-22
lines changed

_posts/2018-05-06-react-props-and-proptypes.markdown

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ tags: [react, reactjs, learn react]
1111

1212
React is built around the concept of components. The components get many specific attributes just like a XML syntax and HTML tag does.
1313

14-
The attributes are called "props" in ReactJS and can be of any type. It can be a string, function or an Array, as long as its valid javascript you can use it as a prop.
14+
The attributes are called "props" in React and can be of any type. It can be a string, function or an Array, as long as its valid javascript you can use it as a prop.
1515

1616
```javascript
1717
<MyComponent size={24} position="fixed" />
@@ -91,11 +91,6 @@ Exercises:
9191
class Profile extends React.Component {
9292
constructor(props) {
9393
super(props)
94-
this.state = {
95-
name: 'This is a default prop',
96-
age: 0,
97-
activate: true
98-
}
9994
}
10095

10196
render() {
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
layout: post
3+
title: React - Component Lifecycle Mounting
4+
date: 2018-05-08
5+
description:
6+
img: react-component-lifecycle-updating.png
7+
tags: [react, reactjs, learn react]
8+
---
9+
10+
React updating have 5 methods:
11+
12+
* UNSAFE_componentWillReceiveProps(object nextProps)
13+
14+
* shouldComponentUpdate(object nextProps, object nextState)
15+
16+
* UNSAFE_componentWillUpdate(object nextProps, object nextState)
17+
18+
* componentDidUpdate(object prevProps, object prevState, object snapshot)
19+
20+
* getSnapshotBeforeUpdate(object prevProps, object prevState).
21+
22+
Let go into details:
23+
24+
#### UNSAFE_componentWillReceiveProps
25+
26+
is invoked before a mounted component receives new props. If you need to update the state in response to prop changes (for example, to reset it), you may compare `this.props` and `nextProps` and perform state transitions using `this.setState()` in this method.
27+
28+
If a parent component causes your component to re-render, this method will be called even if props have not changed. Make sure to compare the current and next values if you only want to handle changes.
29+
30+
React doesn’t call UNSAFE_componentWillReceiveProps() with initial props during mounting. It only calls this method if some of component’s props may update. Calling this.setState() generally doesn’t trigger UNSAFE_componentWillReceiveProps().
31+
32+
#### shouldComponentUpdate(object nextProps, object nextState) boolean
33+
34+
Use `shouldComponentUpdate()` to let React know if a component's output is not affected by the current change in state or props.
35+
36+
is invoked before rendering when new props or state are being received. Defaults to `true`. This method is not called for the initial render or when forceUpdate() is used.
37+
38+
This method return true by default.
39+
40+
Returning false does not prevent child components from re-rendering when their state changes.
41+
42+
If shouldComponentUpdate() returns false, then UNSAFE_componentWillUpdate(), render(), and componentDidUpdate() will not be invoked.
43+
44+
If you determine a component is slow after profiling, you may change it to inherit from `React.PureComponent` which implements shouldComponentUpdate() with a shallow prop and state comparison.
45+
46+
#### UNSAFE_componentWillUpdate(object nextProps, object nextState)
47+
48+
is invoked just before rendering when new props or state are being received. Use this to perform preparation before an update occurs. This method is not called for the initial render.
49+
50+
You cannot call this.setState() here in this method.
51+
52+
You should do anything else in this method (e.g. dispatch a Redux action) that would trigger an update to a React component before `UNSAFE_componentWillUpdate()` returns.
53+
54+
#### componentDidUpdate(object prevProps, object prevState, object snapshot)
55+
56+
is invoked immediately after updating occurs. This method is not called for the initial render.
57+
58+
Use this as an opportunity to operate on the DOM when the component has been updated. This is also a good place to do network requests as long as you compare the current props to previous props (e.g. a network request may not be necessary if the props have not changed).
59+
60+
If your component implements the `getSnapshotBeforeUpdate()` lifecycle, the value it returns will be passed as a third “snapshot” parameter to `componentDidUpdate()`. (Otherwise this parameter will be undefined.)
61+
62+
Noted: `componentDidUpdate()` will not be invoked if shouldComponentUpdate() returns false.
63+
64+
#### getSnapshotBeforeUpdate(object prevProps, object prevState)
65+
66+
is invoked right before the most recently rendered output is committed to e.g. the DOM. It enables your component to capture current values (e.g. scroll position) before they are potentially changed. Any value returned by this lifecycle will be passed as a parameter to `componentDidUpdate()`.
67+
68+
For example:
69+
70+
```javascript
71+
class ScrollingList extends React.Component {
72+
constructor(props) {
73+
super(props);
74+
this.listRef = React.createRef();
75+
}
76+
77+
getSnapshotBeforeUpdate(prevProps, prevState) {
78+
// Are we adding new items to the list?
79+
// Capture the scroll position so we can adjust scroll later.
80+
if (prevProps.list.length < this.props.list.length) {
81+
const list = this.listRef.current;
82+
return list.scrollHeight - list.scrollTop;
83+
}
84+
return null;
85+
}
86+
87+
componentDidUpdate(prevProps, prevState, snapshot) {
88+
// If we have a snapshot value, we've just added new items.
89+
// Adjust scroll so these new items don't push the old ones out of view.
90+
// (snapshot here is the value returned from getSnapshotBeforeUpdate)
91+
if (snapshot !== null) {
92+
const list = this.listRef.current;
93+
list.scrollTop = list.scrollHeight - snapshot;
94+
}
95+
}
96+
97+
render() {
98+
return (
99+
<div ref={this.listRef}>{/* ...contents... */}</div>
100+
);
101+
}
102+
}
103+
```
104+
105+
In the above examples, it is important to read the `scrollHeight` property in `getSnapshotBeforeUpdate` rather than `componentWillUpdate` in order to support async rendering. With async rendering, there may be delays between “render” phase lifecycles (like `componentWillUpdate` and render) and “commit” phase lifecycles (like `getSnapshotBeforeUpdate` and `componentDidUpdate`). If a user does something like resize the browser during this time, a `scrollHeight` value read from `componentWillUpdate` will be stale.

_site/index.html

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ <h3 class="contact-title">Contact</h3>
110110
</aside> <!-- End Sidebar -->
111111
<div class="content-box clearfix">
112112

113+
<article class="post">
114+
115+
<a class="post-thumbnail" style="background-image: url(/assets/img/react-component-lifecycle-updating.png)" href="/react-component-lifecycle-updating/"></a>
116+
117+
<div class="post-content">
118+
<h2 class="post-title"><a href="/react-component-lifecycle-updating/">React - Component Lifecycle Mounting</a></h2>
119+
<p>React updating have 5 methods: UNSAFE_componentWillReceiveProps(object nextProps) shouldComponentUpdate(object nextProps, object nextState) UNSAFE_componentWillUpdate(object nextProps, object nextState)...</p>
120+
<span class="post-date">2018, May 08&nbsp;&nbsp;&nbsp;—&nbsp;</span>
121+
<span class="post-words">4 minute read</span>
122+
</div>
123+
</article>
124+
113125
<article class="post">
114126

115127
<a class="post-thumbnail" style="background-image: url(/assets/img/react-component-lifecycle-mounting.png)" href="/react-component-lifecycle-mounting/"></a>
@@ -190,19 +202,7 @@ <h2 class="post-title"><a href="/react-state/">React - State</a></h2>
190202
<h2 class="post-title"><a href="/react-props-and-proptypes/">React - Props and PropTypes</a></h2>
191203
<p>Props React is built around the concept of components. The components get many specific attributes...</p>
192204
<span class="post-date">2018, May 05&nbsp;&nbsp;&nbsp;—&nbsp;</span>
193-
<span class="post-words">4 minute read</span>
194-
</div>
195-
</article>
196-
197-
<article class="post">
198-
199-
<a class="post-thumbnail" style="background-image: url(/assets/img/react-components.png)" href="/react-components/"></a>
200-
201-
<div class="post-content">
202-
<h2 class="post-title"><a href="/react-components/">React - Components</a></h2>
203-
<p>Components let you split the UI into independent, reusable pieces. Components are like JavaScript functions;...</p>
204-
<span class="post-date">2018, May 05&nbsp;&nbsp;&nbsp;—&nbsp;</span>
205-
<span class="post-words">6 minute read</span>
205+
<span class="post-words">3 minute read</span>
206206
</div>
207207
</article>
208208

_site/sitemap.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
<lastmod>2018-05-08T00:00:00+07:00</lastmod>
5050
</url>
5151
<url>
52+
<loc>/react-component-lifecycle-updating/</loc>
53+
<lastmod>2018-05-08T00:00:00+07:00</lastmod>
54+
</url>
55+
<url>
5256
<loc>/</loc>
5357
</url>
5458
<url>

_site/tags/index.html

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ <h1>Tags in Blog</h1>
118118
<!-- cycles through tag list and creates header row of all tags used in site with accompanying per-tag counts...-->
119119

120120

121-
<li ><a href="#learn+react" class="tag">learn react <span>(12)</span></a></li>
121+
<li ><a href="#learn+react" class="tag">learn react <span>(13)</span></a></li>
122122

123123

124-
<li ><a href="#react" class="tag">react <span>(12)</span></a></li>
124+
<li ><a href="#react" class="tag">react <span>(13)</span></a></li>
125125

126126

127-
<li ><a href="#reactjs" class="tag">reactjs <span>(12)</span></a></li>
127+
<li ><a href="#reactjs" class="tag">reactjs <span>(13)</span></a></li>
128128

129129
</ul>
130130
<!--cycles through tag list and creates subheader for each tag name...-->
@@ -133,6 +133,11 @@ <h1>Tags in Blog</h1>
133133
<h2 id="learn+react">learn react</h2>
134134
<!-- lists all posts corresponding to specific tag...-->
135135

136+
<div class="tag-list">
137+
<span><a href="/react-component-lifecycle-updating/">React - Component Lifecycle Mounting</a></span>
138+
<small><span>| 08 May 2018</span></small>
139+
</div>
140+
136141
<div class="tag-list">
137142
<span><a href="/react-component-lifecycle-mounting/">React - Component Lifecycle Mounting</a></span>
138143
<small><span>| 08 May 2018</span></small>
@@ -198,6 +203,11 @@ <h2 id="learn+react">learn react</h2>
198203
<h2 id="react">react</h2>
199204
<!-- lists all posts corresponding to specific tag...-->
200205

206+
<div class="tag-list">
207+
<span><a href="/react-component-lifecycle-updating/">React - Component Lifecycle Mounting</a></span>
208+
<small><span>| 08 May 2018</span></small>
209+
</div>
210+
201211
<div class="tag-list">
202212
<span><a href="/react-component-lifecycle-mounting/">React - Component Lifecycle Mounting</a></span>
203213
<small><span>| 08 May 2018</span></small>
@@ -263,6 +273,11 @@ <h2 id="react">react</h2>
263273
<h2 id="reactjs">reactjs</h2>
264274
<!-- lists all posts corresponding to specific tag...-->
265275

276+
<div class="tag-list">
277+
<span><a href="/react-component-lifecycle-updating/">React - Component Lifecycle Mounting</a></span>
278+
<small><span>| 08 May 2018</span></small>
279+
</div>
280+
266281
<div class="tag-list">
267282
<span><a href="/react-component-lifecycle-mounting/">React - Component Lifecycle Mounting</a></span>
268283
<small><span>| 08 May 2018</span></small>
448 KB
Loading

0 commit comments

Comments
 (0)