-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact
More file actions
359 lines (247 loc) · 9.46 KB
/
react
File metadata and controls
359 lines (247 loc) · 9.46 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
getIntialState function
getDefaultProps
componentWillMount - right before rendering
componentDidMount - right after component renders
Access DOM, integrate with frameworks, set timers, AJAX request
componentWillReciveProps - when receiving new props, not called on intial render
shouldComponentUpdate - Before render when new props or state
are being recieved but not on intial render
if state changes but there will be no DOM change return false from this
componentWillUPdate - Imdeiately beofe rendering when
new props or state are being recieved, not on intial render,
can't call setstate in this functino
componetDidUPdate - called right after updates are rendered to the DOM,
not on intial render though
for working on DOM after updates
componentWillUnmount - right before componet is removed from the DOM
used for cleanup
keys for dynamic components
propTypes - a map to validate props before rendering a component
these are only run in development mode for performance sake
if a propType is not required it should have field in getDefaultProps
propTypes: {
prop1: React.PropTypes.object.isRequired,
prop2: React.PropTypes.func.isRequired,
prop3: React.PropTypes.object.isRequired,
prop4: React.PropTypes.object.isRequired,
prop5: React.PropTypes.object.isRequired
}
minified version of react runs in producion mode by default
mixins - useful for "cross cutting concerns" or sharing code between components
.................Redux...............
Why use Redux?
One Store
Reduced Boilerplate
Isomorphic/Universal Friendly (rendering on server)
Immutable Store
Hot Reloading
Time-travel debugging
Small api
Magic over Boilerplate
Flux has more boilerplate, Redux has nore magic (but not to much)
........................Flux........................
Flux is a pattern for unidirectional data flow
All updates to app state occur via a centralized dispacher
Unideriectional data flow (no two way binding)
You will need a flux dispacher and a JS event library
Two way binding
viewmodle -> view
viewmodle <- view
Uniderectional
Action -> dispacher -> store -> React View -> Action
Actions - user interactions that occur in your react component
Dispacher - "singleton registry" that hanldes actions
really just a centralized list of callbacks
notifies everyone who cares
stores - holds app state
when store changes react components update
Actions encapsulate specific events that occur in the app
Dispacher triggers a method that allows us to trigger a dispach
to the stores and trigger a payload of data wich is called an
action
ACtion creators - dispacher helper methods, they describe all
actions possible in the application
Actions happen two places,
1. when user itneracts with the UI, view trigers action
2. server triggers something
(Possible 3. app itself.. say a timer or an AI actor)
Action payloads have common structure of type and data
{
type: USER_SAVED
data: {
firstName: Marty,
lastName: McFly
}
}
Dispacher - there is a singlton meaning there is only one per app
dispacher dispaches actions
Stores register with the dispacher so they can be notified when data changes
dispacher has a list of callbacks it ivokes, dispaches payload from action
Stores hold, app state, lgic, data retrieval
a store is not a model of an MVC instead it contains models
app can have a single store or multi stores
stores get updated with callbacks they register with dispacher
only stores are allowed to register callbacks with dispacher, NOT compoents
flux stores are extedned with Node's EventEmitter - allows stores to both
listen to and brodcast events
stores have no direct setter methods, they only accept updates via callbacks
registered with dispacher
store is the only thing in applicaton that knows how to update data
Core interface of Store
1. Extend EventEmitter
2. addChangeListener and remove ChangeListener
3. emitChange
React compoents listen to the stores, stores emmit changes via Node's event
emmiter
Controller Views should be the components interaction with flux stores
Actions make web api calls??
Dispacher functions
register(function callback)
store says run this when action happens
unregiester(function callback)
store says foreget about this action I don't care anymore
waitFor(array<string>ids)
store says "update this store first"
dispach(object payload)
action says "tell stores about this"
isDispaching()
bool saying dipspacher is busy dispaching
Different from Pub-Sub
1. every payload is dispached to all registerd callbacks
2. callbacks can wait for other callbacks
.......Four Ways to Declare React Components........
createClass
ES6 Class
no autobinding
PropTypes and Default props declared separately from class declaration
setInitialState function not used, use constructor instead
ES5 funcitonal stateless
ES6 functional stateless
Stateless components
no state,
does not utilized lifecycle methods
no performacne optimization
Benefits:
No class needed
avoide 'this' keyword
enforce best practice of stateless Presentational Components
simpler, less noise
with desructuring all props used are declared in a simple way
allowing for code completion and intellisense also
bloated compoents are obvious
descructuring props shows if there are to many props
Easy to understand
Easy to test
Performance will come down the road
When to use Class components:
need State
need reference to DOM
need lifceycle mehtods
using child functions
functions within functions can hurt performance in stateless func components
every render creates a new instance of that function
what exactly do you mean by nesting funtions??
Other ways to create compoents
Object.create
Mixins
Parasitic Component
StampIt
bit.ly/react-define-component
Container Components
concerned with behaivior
little to no markup
in redux usually created through redux connect function at bottom of file
pass data and actions down
knows about redux
typically stateful
Presentation
Nearly all markup
receives data via props
does't know aobut redux
typically statless function
other names
conainer vs presentaitons
smart vs dumb
stateful vs stateless
controller view vs view
If you notice some components do't use props they recieve but merely forward
them down it's a good time to introduce container components
Container compoents do not only need to be at the top, sometimes they are useful
inside other components
...........More Redux............
Why use Redux?
Complex data flows
Inter-component communication
Non-heirarchical data (?)
Many actions (a lot of things going on)
Same data used in multiple places
same data but not a simple parent child relationship
"...if you aren't sure you need it, you don't need it."
3 core pricnciples
1. All state is in single imutalbe store
2. Only way to change state, emmit an action
3. State is changed by pure functions, aka Reducers
Flux Redux commonality
Unidirection data flows - Data down, Actions up
Finite set of actions that define how state changes
Both have stores, Flux has muti, Redux has one
Redux New conepts
Reducers state + action = new state
pure functions
Containers - special container React components
necessary logic for marshalling data and actions they pass down to
dumb components via props
Immutability - redux store is imutable
must work with imutable data in reducers
Redux does not have a dispacher at all like Flux does, each action is handled
by one or more Reducers wich update the single store
Action -> Store
Store -> Reducers
Store <- Reducer
Store -> React -> Action
Flux
Stores contain state and change logic
Multi Stores
Flat and disconnected stores
dispacher at center of applicaton connects actions to stores
you have to explicitly subscribe your react views to change handlers
flux you manipulate state directly, it imutable
Redux
Store and change logic are separate
all logic exist in Reducer
One Store
easier to deal with state interaction
one source of truth
Single store with hierarchical reducers
gives you nesting of your reducers (eh?)
No dispacher
reduxes singel store passes actions to the reducers you define
it does this trhough a root reducer you define
React redux - library that connects your react components to the redux store
automatically
has a connect method that generates a top level react component that's
connected to your actions in store, everytime the store state changes
a function tirggers a re-render on your component
state is imutable - instead of changing state you return an updated copy of
state
Action is triggered has type and data
Reducer checks the action type and uses data to return new store
Store is updated
React-redux notifies relavant components
..........Not React specific...........
What does my env need to do?
Automated Testing
Linting
Minification
Bundling
JSX compilation
ES6 transpiling
One Command!
eslint note:
0 off
1 warning
2 error
testSetup
process.env.NODE_ENV = 'test';
//npm script trick - run multiple scripts in parallel
"scriptName" : npm-run-all --parallel script1 script2 script3