forked from kay-is/react-from-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
11-lifecycle-methods.html
115 lines (84 loc) · 3.37 KB
/
11-lifecycle-methods.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!doctype html>
<title>11 Lifecycle Methods - React From Zero</title>
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/[email protected]/create-react-class.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<div id="app"></div>
<script type="text/babel">
// If we use component classes, our components inherit
// a bunch of methods, which get called by React at specific
// times to allow us to get more control over our components
// a few of them we already met in lesson 9
// Here are a few new ones. Not all of them, but the most important ones
var TRANSLATION_FROM_SOMEWHERE = "Text from a synchronous source."
var MyComponent = createReactClass({
// This method is for default prop values
// it gets called before the props are given to our component
// the "real" props override them if there are any
getDefaultProps: function() {
return {
iGetOverriden: "default",
iStayAsIAm: "default",
}
},
// This method is called before a component got mounted to the DOM
// it returns values that are used for this.state
getInitialState: function() {
return {serverData: null}
},
// This method gets called right before the component is mounted
// can be used to initialize some synchronous configuration, that should
// be available before the component renders
componentWillMount: function() {
this.TEXT = TRANSLATION_FROM_SOMEWHERE
},
// This method will be called right after the component got mounted
// it's a good place to start some asynchronous tasks.
// For example on the first mount it shows a loading message
// then componentDidMount is called and gets some server data.
componentDidMount: function() {
var component = this
// We clean up the data and get new from somewhere
function loadData() {
component.setState({serverData: null})
getServerData(function(data) {
component.setState({serverData: data})
})
}
// Initial data load
loadData()
// We simulate a server request every 4 seconds
this.updateInterval = setInterval(loadData, 4000)
},
// This method will be called before the component gets removed from the DOM
// a bit like a destructor. Here we can do some cleanup.
componentWillUnmount: function() {
clearInterval(this.updateInterval)
},
// This method is called before a render when new props or state is available
// it won"t be called on the first render or if this.forceUpdate() is used
// it can be used if some state or prop changes don"t require a rerender
shouldComponentUpdate: function(nextProps, nextState) {
// we want to render on every change, this is the default behaviour
return true
},
render: function() {
return (
<h2 style={{width: 400, margin: "auto"}}>
Overriden Prop: {this.props.iGetOverriden}<br/><br/>
Default Prop: {this.props.iStayAsIAm}<br/><br/>
{this.TEXT}<br/><br/>
{this.state.serverData
? this.state.serverData
: "Loading..."
}
</h2>
)
},
})
function getServerData(fn) {
setTimeout(function() { fn("Data Loaded!") }, 700)
}
ReactDOM.render(<MyComponent iGetOverriden={"override"}/>, document.getElementById("app"))
</script>