Skip to content

Commit 462bc5f

Browse files
committed
constructor vs componentWillMount()
1 parent 21d8526 commit 462bc5f

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

React/Component-Life-Cycle/LifecycleMethods-quick-Notes.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ componentDidMount()
2323

2424
---
2525

26+
#### componentDidMount() is the best place to put calls to fetch data (as against componentWillMount ), for two reasons:
27+
28+
- Using DidMount makes it clear that data won’t be loaded until after the initial render. This reminds you to set up initial state properly, so you don’t end up with undefined state that causes errors.
29+
- If on the other hand, I put a fetch network call inside componentWillMount - then a situation may arise when an asynchronous call to fetch data will not return before the render happens. This means the component will render with empty data at least once. There is no way to “pause” rendering to wait for data to arrive.
30+
31+
- If you ever need to render your app on the server (SSR/isomorphic/other buzzwords), componentWillMount will actually be called twice – once on the server, and again on the client – which is probably not what you want. Putting the data loading code in componentDidMount will ensure that data is only fetched from the client.
32+
2633
## Updating
2734

2835
##### componentWillReceiveProps
@@ -107,4 +114,4 @@ componentWillUnmount()
107114
```
108115
- DOM cleanup
109116
- listener removal & timer removal
110-
117+

React/Component-Life-Cycle/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@ For the first category, the component may need to perform some/all of the follow
1616

1717
* Set up or remove any listeners or web-sockets (like a firebase ref listener) - start listener within componentDidMount and stop listener in componentWillUnmount
1818

19+
20+
### Mounting
21+
22+
- constructor()
23+
- componentWillMount()
24+
- render()
25+
- componentDidMount()
26+
27+
### Updating
28+
29+
- componentWillReceiveProps()
30+
- shouldComponentUpdate()
31+
- componentWillUpdate()
32+
- render()
33+
- componentDidUpdate()
34+
35+
### Unmounting
36+
37+
componentWillUnmount()
38+
39+
1940
Now coming to the life cycle events that triggered when the component receives new data from its parent component.
2041

2142
* `componentWillReceiveProps` - for times when you want to execute some code whenever your component receives props
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### componentWillMount
2+
3+
This function is called right before the component’s first render
4+
5+
## Difference between constructor and componentWillMount
6+
7+
Only thing you can't achieve inside the constructor that you can with ComponentWillMount is to setState() also react throws a warning if anything inside your constructor modifies state even in another component.

0 commit comments

Comments
 (0)