-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
385 lines (290 loc) · 9.38 KB
/
index.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<!DOCTYPE html>
<html>
<head>
<title>React.js and Flux architecture</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link href="./assets/css/base.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.remark-inline-code{
font-size: 18px;
color: #333;
background: #f8f8f8;
}
</style>
</head>
<body>
<header>
<img class="epam-logo" src="./assets/img/logo.png"/>
<h1>React.js and Flux architecture</h1>
</header>
<!-- ================== Slides ================== -->
<textarea id="source">
class: center, middle
.title[
Front-end training
# React.js and Flux architecture
]
---
# 1 percent but growing!
<img width='500px' src='assets/img/it-angular.png' />
<img width='500px' src='assets/img/it-react.png' />
<small>Screenshots from <a href="http://www.itjobswatch.co.uk/">http://www.itjobswatch.co.uk/</a></small>
---
# React.js
<img width='120px' src="assets/img/react-logo.png" />
React.js is a V in MVC
React.js key ideas:
- virtual DOM
- component framework
What makes React special:
- simple to learn
- fast rendering
- composability
- no direct DOM manipulation
---
# Bootstrapping
1. Get latest version of react.js (100k minified, 31k minified and gzipped)
2. Render root React component into DOM:
```javascript
React.render(<YourRootComponent />, document.getElementById('id1'));
```
It's possible to have multiple root nodes.
---
# React building blocks
- React DOM Elements - wrappers around DOM elements.
```javascript
<input key={i} className="my-input" type="text" ref="myInput" />
```
- React Components - widgets with custom attributes (props) and local state.
```javascript
<MyInput key={i} className="my-input" product={{name: "Bacon"}} onRemove={handleRemove} />
```
<br />
React application is a tree of React DOM Elements and React Components
```javascript
<div> // React DOM Element
<Products /> // React Component
<Cart> // React Component
<span>inside</span> // React DOM Element
</Cart>
</div>
```
---
# JSX
- without JSX you have to create React Elements
```javascript
var YourRootComponent = React.createClass({
render: function() {
return React.createElement('div', null, "Hello world" + new Date())
})
```
- with JSX React Elements are created for you
```javascript
var YourRootComponent = React.createClass({
render: function() {
return <div>Hello world {date={new Date()}}</div>
})
```
With JSX there's less code
---
# Component api
React component API is the most complicated part of React.js.
```javascript
var YourComponent = React.createClass({
// Component-specific state
getInitialState // initial components state
getDefaultProps // default props values
propTypes // Strict typed components!
mixins // Add mixins, which are smart about Lifecycle methods
// Lifecycle methods
componentWillMount
componentDidMount // Opportunity for component's DOM manipulation
componentWillReceiveProps // Opportunity to set component's state
componentWillUnmount // Opportunity to unregister event listeners
...
render: function() { // Builds Virtual DOM
return <div>Hello world {date={new Date()}</div>
}
})
```
'render' is the only required method, it's return value is used to build Virtual DOM
---
# React.js data flow
Data is stored as Components' State.
```javascript
component.setState({selected: true})
```
"Data down, Events up"
```javascript
var ItemsList = React.createClass(
getInitialState: function() {
return {items: ["item1", "item2", "item3"]}
},
_handleItemRemove: function(removedItem) {
var filteredItems = this.state.items.filter(
function(item) {return item !== removedItem}
)
this.setState({items: filteredItems});
}
render: function() {
// <Item /> component can access this.props.onRemove, this.props.items
return this.state.items.map(function(item, i) {
<Item onRemove={this._handleItemRemove} item={item} />
})
})
}
```
---
# React change detection
component.setState(...) -> newVirtualDOM/existingVirtualDOM comparison -> smart DOM rerender
<img width='320px' style='float: right' src='assets/img/virtual-dom.png' />
Ways to improve performance:
- call setState as low as possible
- use shouldComponentUpdate to prevent re-rendering of large sub-tree
- compare only State component receives with immutable values and PureRenderMixin.
Optimize performance by preventing Virtual DOM rerendering:
<img width='320px' style='float: right' src='assets/img/virtual-dom-optimisation.png' />
---
# Nice features. #1 - Ease of composability
```javascript
...
render: function() {
return (
<BaseModal
closeBtn={true}
onClose={this.props.onClose}
isOpen={this.props.isOpen}
>
<form>
<div className='input-pair'>
<div className='input-pair__title-box'>
NAME
</div>
<div className='input-pair__input-box'>
<MacSelect
name='country'
options={[{value: 'Ukraine', label: 'Ukraine'}, ...]}
value={this.state.storedParams.country}
onChange={this._onInputChange}
/>
</div>
</div>
...
```
---
# Nice features. #2 - No templating language
React Component instances are manipulated using plain old JavaScript
```javascript
...
render: function() {
return (
<div className='assets-grid'>
<div className='assets-grid__items'>
{this.state.assets.map(function(asset, index) {
return <AssetGridItem key={index} asset={asset} />
})}
</div>
</div>
);
}
...
var obj = {
a: <div>hello</div>,
b: <ListItem />
};
var listItem = predicate ? <ListItemA /> : <ListItemB />
```
---
# Nice features. #3 - PropTypes
PropTypes provide strict typing for component's parameters
```javascript
var YourComponent = React.createClass(
...
propTypes: function() {
return {
size: React.PropTypes.number,
position: React.PropTypes.string.isRequired,
contentType: React.PropTypes.oneOf(['News', 'Photos']),
message: React.PropTypes.instanceOf(Message)
}
}
...
)
```
---
# Debugging tools
<img width='800px' src='assets/img/react_debug.png' />
<a href="https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en">Chrome addon</a>
---
# Roadmap to convincing boss to use React.js
- Facebook, Instagram, Khan Academy, New York Times use it
- Simpler code
- Ability to incrementally add it to existing projects
- Fast rendering speed
- Code sharing between backend and frontend (isomorphic apps)
---
# Live coding - React
State is passed from top to bottom of React Component tree.
<img width='1000px' src='assets/img/workflow.png' />
---
# Flux architecture
![MVC-js](assets/img/flux-drawing.jpg)
<small>https://twitter.com/abdullin/status/553292397506220034/photo/1</small>
Key features:
- Unidirectional data flow
- Flux stores are domain models, not ORM models
- Flux is more similar to CQRS than MVC
---
# Components
<img width='600px' src='assets/img/flux_component.png' />
Components can only call Action Creators and cannot modify Stores directly.
Example:
```javascript
actions.addProductToCart(this.props.product);
```
---
# Action creators
<img width='600px' src='assets/img/flux_action_creator.png' />
Example:
```javascript
loadMoreSearchResults: function(parameters) {
dispatcher.dispatch(constants.LOAD_MORE_SEARCH_RESULTS_PROGRESS);
searchDao.advancedSearch({parameters: parameters}).then(function(results) {
dispatcher.dispatch(constants.LOAD_MORE_SEARCH_RESULTS_SUCCESS, results);
}));
},
```
---
# Dispacher
There's single Dispatcher instance per application
<img width='600px' src='assets/img/dispatcher.png' />
Dispatcher is not simple event-bus - it provides synchronization between Stores' callbacks.
```javascript
CityStore.dispatchToken = flightDispatcher.register(function(payload) {
if (payload.actionType === 'country-update') {
flightDispatcher.waitFor([CountryStore.dispatchToken]);
CityStore.city = getDefaultCityForCountry(CountryStore.country);
}
});
```
---
# Stores
<img width='600px' src='assets/img/flux_store.png' />
Store is a wrapper for Model with separate write and read interfaces.
- write interface methods are invoked after matching Dispatcher Actions are triggered;
- read interface corresponds to UI needs.
Stores don't mutate each other directly.
---
# Live coding - Flux
- replace top-to-bottom Data flow with Flux architecture
- add 'Remove from Cart' button to Product component
<img width='950px' src='assets/img/workflow.png' />
---
# The end
Learn more about React.js Flux architecture. It will help you become a better programmer.
</textarea>
<!-- ================== End of Slides ================== -->
<script src="./assets/js/remark-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">remark.create({highlightStyle: 'github'});</script>
</body>
</html>