This repository was archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact.txt
More file actions
419 lines (333 loc) · 9.26 KB
/
react.txt
File metadata and controls
419 lines (333 loc) · 9.26 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
React router
egghead - react fundamentals
youtube - react in 15ish minutes
react doc
DIFF POLYMER
With JSX you can write JS mixed with HTML. Need to place inside {}
In Polymer, you need to have data bindings {{}} or {{{}}} or [[]].
Example: javascript map function to creates a JSX array
["a", "b", "c"] -->
[<li>a</li>, <li>b</li>, <li>c</li>]
Does get messy with all the {} in react.
Good practice to pass a unique key attr to all components.
In Polymer, all elements with ids are accessable through this.$.nisse. Dynamic
elements are available through selector this.$$. In react ->
<input placeholder="Name:" ref={(input) => {this._author = input}} />
Use concat instead of push to make react fast.
One-way control flex-flow
Parent passes functions through props to children.
Parent passes author/body through props to children.
Child calls callback.
Component - call render generates virtualdom. (step1)
virtualdoom - in-memory repr.
step2 - display in browser through diffing.
diffing - only the least area updates.
COMPONENT
Must inherit from React.Component
Must have render func that returns jsx markup code.
JSX - no quotes needed.
functions with _ are custom (not react)
RENDERER
ReactDOM.render
Classname param1.
ReactDOM.render(<StoryBox />, document.getElementById('story-app'))
Takes output from react component and add to the DOM.
RESOURCES
script : react.js, react-dom.js, babel.js (es6, jsx)
script type="text/babel" src="components.js"
------
class RobotBox extends React.Component {
render() {
return <div>Hello From React</div>
}
}
let target = document.getElementById('robot-app');
ReactDOM.render( <RobotBox/>, target);
------
JSX
Lower case = HTML elements
Upper CamelCase = React component
render() {
return <div>Hello From React</div>
}
--->
render() {
return React.createElement('div', null, 'Story Box');
}
The word 'class' is reserved in JS, thus use className
instead
<div>
<h3>Bla</h3>
<p className="lead">bla</p>
</div>
--->
return React.createElement('div', null,
React.createElement('h3', null, "Bla"),
React.createElement('p', null, "bla"));
-----
class RobotTime extends React.Component {
render() {
let pi = Math.PI;
return (
<div className="is-tasty-pie" >
The value of PI is approximately {pi}.
</div>
);
}
}
-----
class RobotItems extends React.Component {
render() {
const topics = ["React", "JSX", "JavaScript", "Programming"];
return (
<div>
<h3>Topics I am interested in</h3>
<ul>
{topics.map(function(topic) {
return <li>{topic}</li>;
})}
</ul>
</div>
);
}
}
-----
Arguments passed to components are called 'props'. Look like HTML attr.
<Comment author="asdasd" body="sdflsdkfjls!"/>
To read
{this.props.author}
----
class Comment extends React.Component {
render() {
return <div className="comment">
<p className="comment-header">Anne Droid</p>
<p className="comment-body">
I wanna know what love is...
</p>
<div className="comment-actions">
<a href="#">Delete comment</a>
</div>
</div>
}
}
---
DYNAMIC DATA
_getComments() {
return commentList.map((comment) => {
return (<Comment author={comment.author} body={comment.body} key={comment.id}/>)
});
}
class CommentBox extends React.Component {
render() {
const comments = this._getComments();
return (
<div>
...
{comments.length}
{comments} <---JSX knows howto render arrays!
</div>
)
}
}
-----
class CommentBox extends React.Component {
render() {
const comments = this._getComments() || [];
return(
<div className="comment-box">
<h3>Comments</h3>
{this._getPopularMessage(comments.length)}
<h4 className="comment-count">{comments.length} comments</h4>
<div className="comment-list">
{comments}
</div>
</div>
);
}
_getPopularMessage(commentCount) {
const POPULAR_COUNT = 10;
if (commentCount > POPULAR_COUNT) {
return (<div>
This post is getting really popular, don't miss out!
</div>)
}
}
_getComments() {
const commentList = [
{ id: 1, author: 'Clu', body: 'Just say no to love!', avatarUrl: 'images/default-avatar.png' },
{ id: 2, author: 'Anne Droid', body: 'I wanna know what love is...', avatarUrl: 'images/default-avatar.png' }
];
return commentList.map((comment) => {
return <Comment author={comment.author} body={comment.body} avatarUrl={comment.avatarUrl} key={comment.id}/>
});
}
}
class Comment extends React.Component {
render() {
return(
<div className="comment">
<img src={this.props.avatarUrl} alt={`${this.props.author}'s picture`}/>
<p className="comment-header">{this.props.author}</p>
<p className="comment-body">
{this.props.body}
</p>
<div className="comment-actions">
<a href="#">Delete comment</a>
</div>
</div>
);
}
}
-----
STATE
1) jQuery, Backbone (direct dom manip)
2) React (indirect dom manip)
ex:
$('.stuff').on('click', function() {
});
ex:
if (this.state.showComments) {
}
ex:
constructor() {
super();
this.state = {
showComments: false
}
}
render() {
let nodes;
if (this.state.showComments) {
nodes = <div>....
}
return (
...
{nodes}
)
}
// Use setState, this will trigger a re-render.
this.setState({showComments: true});
this.setState({isAbusive: !this.state.isAbusive});
-------------
class CommentBox extends React.Component {
render() {
const comments = this._getComments() || [];
return(
<div className="comment-box">
<h3>Comments</h3>
{this._getPopularMessage(comments.length)}
<h4 className="comment-count">{this._getCommentsTitle(comments.length)}</h4>
<div className="comment-list">
{comments}
</div>
</div>
);
}
_getPopularMessage(commentCount) {
const POPULAR_COUNT = 10;
if (commentCount > POPULAR_COUNT) {
return (
<div>This post is getting really popular, don't miss out!</div>
);
}
}
_getComments() {
const commentList = [
{ id: 1, author: 'Clu', body: 'Just say no to love!', avatarUrl: 'images/default-avatar.png' },
{ id: 2, author: 'Anne Droid', body: 'I wanna know what love is...', avatarUrl: 'images/default-avatar.png' }
];
return commentList.map((comment) => {
return (<Comment
author={comment.author}
body={comment.body}
avatarUrl={comment.avatarUrl}
key={comment.id} />);
});
}
_getCommentsTitle(commentCount) {
if (commentCount === 0) {
return 'No comments yet';
} else if (commentCount === 1) {
return '1 comment';
} else {
return `${commentCount} comments`;
}
}
}
class Comment extends React.Component {
constructor() {
super();
this.state = {
isAbusive: false
};
}
render() {
let commentBody;
if (this.state.isAbusive) {
commentBody = this.props.body;
} else {
commentBody = <em>Content marked as abusive</em>;
}
return(
<div className="comment">
<img src={this.props.avatarUrl} alt={`${this.props.author}'s picture`} />
<p className="comment-header">{this.props.author}</p>
<p className="comment-body">
{commentBody}
</p>
<div className="comment-actions">
<a href="#">Delete comment</a>
<a onClick={this._toggleAbuse.bind(this)} href="#">Report as Abuse</a>
</div>
</div>
);
}
_toggleAbuse(event) {
event.preventDefault();
this.setState({isAbusive: !this.state.isAbusive});
}
}
-------------
SYNTETIC EVENTS
Add a ref to the element (accessable after rendered)
<input ref={(input) => this._author = input}/>
React calls the function when render.
Parent -> Child (gives function)
React unifies all browser events (syntetic events)
// Both get and set (in state)
_getCharacterCount() {
this.setState({characters: this._body.value.length})
}
REMOVE SERVERS
Set initial data to empty array []
constructor() {
this.state = {
comments = []
}
}
componentWillMount() {
this._fetch();
}
componentDidMount() {
// Browser dom is only updated if changed.
this._timer = setInterval(() => this._fetch(), 5000);
}
componentWillUnmount() {
clearInterval(this._timer);
}
_fetch() {
jQuery.ajax({
method: 'GET',
url: '',
success: (comments) => {
// this is current class (arrow)
this.setState({comments});
}
});
}
Lifecycle
constructor
componentWillMount (rendered for the first time)
render
componentDidMount
componentWillUnmount