-
Notifications
You must be signed in to change notification settings - Fork 0
/
08_lifecycle.html
160 lines (130 loc) · 5.18 KB
/
08_lifecycle.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono:300,300i,700,700i|Roboto:300,300i,700,700i" rel="stylesheet">
<link rel="stylesheet" href="css/react.css">
<script src="https://unpkg.com/react@latest/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@latest/dist/react-dom.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel" data-presets="es2015,stage-2,react" data-plugins="transform-class-properties">
( function() {
/**
* List
*
* @param {Object} props props thats are passed through
*
* @returns {Object} React Element containing the list and a title
*/
const List = ( props ) => (
<div>
<h1>{ props.title }</h1>
<ul className="list">
{
props.list.map(
item => <li className="list-item" key={ item.id }>{ item.content }</li>
)
}
</ul>
</div>
);
/**
* MyButton
*
* React function component for stateless components
*
* @param {Object} props props that are passed to the component
*
* @return {Object} React Element Object
*/
class MyComponent extends React.Component
{
constructor( props )
{
// run super to run constructor on React.Component
super( props );
// set initial state
this.state = {};
}
fetchData( url, callback )
{
fetch( url )
.then( response => response.json() )
.then( callback );
}
componentDidMount()
{
console.log('%ccomponentDidMount()', 'color:#A53B49')
this.fetchData( this.props.url, json => this.setState( json ) );
}
shouldComponentUpdate( nextProps, nextState )
{
console.group( '1. shouldComponentUpdate(nextProps, nextState)')
console.log( '%cnextState = ', 'color:#476E94', JSON.stringify( nextState, null, 2 ) )
console.log( '%cthis.state = ', 'color:#2F7E3A', JSON.stringify( this.state, null, 2 ) )
console.log( "%cshould update?", 'color:#C2822D', this.state.title !== nextState.title)
console.groupEnd( '1. shouldComponentUpdate(nextProps, nextState)')
if ( this.state.title !== nextState.title )
{
return true;
}
return false;
}
componentWillUpdate(nextProps, nextState)
{
console.group( '2. componentWillUpdate(nextProps, nextState)')
console.log( '%cthis.state =', 'color:#476E94', JSON.stringify( this.state, null, 2 ) );
console.log( '%cnextState = ','color:#2F7E3A', JSON.stringify( nextState, null, 2 ) );
console.groupEnd( '2. componentWillUpdate(nextProps, nextState)')
}
componentDidUpdate(prevProps, prevState)
{
console.group( '4. componentDidUpdate(prevProps, prevState)')
console.log( '--- props ---' )
console.log( '%cprevProps =', 'color:#476E94', JSON.stringify( prevProps, null, 2 ) );
console.log( '%cthis.props =', 'color:#2F7E3A', JSON.stringify( this.props, null, 2 ) );
console.log( '--- state ---' )
console.log( '%cprevState =', 'color:#476E94', JSON.stringify( prevState, null, 2 ) );
console.log( '%cthis.state =', 'color:#2F7E3A', JSON.stringify( this.state, null, 2 ) );
console.groupEnd( '4. componentDidUpdate(prevProps, prevState)')
}
render()
{
console.log( '%crender()', 'color:#A53B49')
if ( !this.state.hasOwnProperty( 'title' ) )
{
return null;
}
return (
<div>
<List { ...this.state } />
<button onClick={ this.handleClick } className="btn">
load more
</button>
</div>
);
}
/**
* handleClick
*
* callback function for button click event
*/
handleClick = () =>
{
this.fetchData( this.props.urlNext, json => this.setState( json ) );
}
}
// render to Component:
ReactDOM.render(
<MyComponent url="data/state.json" urlNext="data/stateNext.json" />,
document.querySelector( '#root' )
);
}() )
</script>
</body>
</html>