1 | <!DOCTYPE html>
|
2 | <html>
|
3 | <head>
|
4 | <title>fz-react-cli docs</title>
|
5 | <meta charset="utf-8">
|
6 | <meta name="viewport" content="width=device-width, initial-scale=1">
|
7 | <link rel="stylesheet" href="./css/b.min.css">
|
8 | <script src="./js/j.min.js"></script>
|
9 | <script src="./js/b.min.js"></script>
|
10 | |
11 |
|
12 | <style>
|
13 | body {
|
14 | position: relative;
|
15 | }
|
16 |
|
17 | script {
|
18 | display: block;
|
19 | }
|
20 | p{
|
21 | width:500px;
|
22 | text-indent: 50px;
|
23 | }
|
24 | .noIntend{
|
25 | text-indent: 0px;
|
26 | }
|
27 | #introduction,#basic,#serverside,#react,#redux,#section41,#section42,#router{
|
28 | padding-top:50px;min-height:650px;
|
29 | }
|
30 |
|
31 | </style>
|
32 | </head>
|
33 | <body data-spy="scroll" data-target=".navbar" data-offset="50">
|
34 |
|
35 | <nav class="navbar navbar-inverse navbar-fixed-top">
|
36 | <div class="container-fluid">
|
37 | <div class="navbar-header">
|
38 | <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
|
39 | <span class="icon-bar"></span>
|
40 | <span class="icon-bar"></span>
|
41 | <span class="icon-bar"></span>
|
42 | </button>
|
43 | <a class="navbar-brand" href="#">FZ-REACT-CLI Help</a>
|
44 | </div>
|
45 | <div>
|
46 | <div class="collapse navbar-collapse" id="myNavbar">
|
47 | <ul class="nav navbar-nav">
|
48 | <li><a href="#introduction">Introduction</a></li>
|
49 | <li><a href="#basic">Basic</a></li>
|
50 | <li><a href="#react">React</a></li>
|
51 | <li><a href="#redux">Redux</a></li>
|
52 | <li><a href="#router">Router</a></li>
|
53 | <li><a href="#serverside">Server Side render</a></li>
|
54 |
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | </ul>
|
62 | </div>
|
63 | </div>
|
64 | </div>
|
65 | </nav>
|
66 |
|
67 | <div id="introduction" class="container-fluid">
|
68 | <h1>Introduction</h1>
|
69 | <p>fz-react-cli is a node cli tool to create and build our react project and also JS library.
|
70 | Before installation kindly check your node version is v6.10.0 or above (node -v)
|
71 | and also check git installed your machine (git --version).<br />
|
72 | Node - <a href="https://nodejs.org/en/download/" target="_blank">Download</a></p>
|
73 | <h3>Installation</h3>
|
74 | <pre>
|
75 | > node -v
|
76 | > git --version
|
77 | > npm install fz-react-cli -g</pre>
|
78 |
|
79 | <h3>How to create react application and how to start the application?</h3>
|
80 | <p>You can create and start the application using the following command. It starts running in the default port 9090.
|
81 | You may change your running port by passing argument --server:port=8080.<br />
|
82 | <pre>
|
83 | > fz-react-cli app <appname>
|
84 | > cd <appname>
|
85 | > npm run start</pre>
|
86 |
|
87 | Some useful arguments
|
88 | <ul>
|
89 | <li>--server:port=9090 <br />(for changing port)</li>
|
90 | <li>--server:context=support || portal <br />(for changing context)</li>
|
91 | <li>--dev:hot=true <br />(for hot reloading while development)</li>
|
92 | <li>--server:prod=true <br />(for checking like production build )</li>
|
93 | </ul>
|
94 | </p>
|
95 |
|
96 | <h3>React application folder structure look like below.</h3>
|
97 |
|
98 | <pre>
|
99 | -<appname>
|
100 | -src
|
101 | -components
|
102 | -<componentnameasfoldername>
|
103 | -__tests__
|
104 | * <componentname>.spec.js
|
105 | -docs
|
106 | * <componentname>__<state1>.docs.js
|
107 | * <componentname>__<state2>.docs.js
|
108 | * <componentname>.js
|
109 | * <componentname>.css
|
110 |
|
111 | * index.js ( component export )
|
112 | * docs.js ( docs export )
|
113 | -containers
|
114 | -<containercomponent>
|
115 | *<containercomponent>.js
|
116 | *<containercomponent>.css
|
117 | * index.js
|
118 | -mockapi
|
119 | * index.js
|
120 | -app
|
121 | * index.html
|
122 | -docs (optional)
|
123 | * all.html (optional)
|
124 | * package.json
|
125 | * .gitignore
|
126 | * README.md
|
127 |
|
128 |
|
129 | Note:
|
130 | - folder
|
131 | * file
|
132 | -<> userfolder
|
133 | *<> userfile
|
134 | </pre>
|
135 | <p>
|
136 | <b>Information:</b> You may create some folder under "components" folder. We can call it as group.
|
137 | Under group folder, we create component then it is specific to that group not generic component.
|
138 | It is under "components" then more generic component. Similarly rule for containers also.
|
139 |
|
140 | </p>
|
141 | <h3>What is Component?</h3>
|
142 | <p>Components are small and reusable building blocks in your application. Example Button is a component.
|
143 | We can use same button throught our application. These components are created under components folder.</p>
|
144 |
|
145 | <h3>How to create component?</h3>
|
146 | <p>Inside components folder create new folder and rename into component name.
|
147 | Create Component name dot css and js file. Create "docs" folder for documentation. create "__tests__" folder for testing purpose. we will discuss docs and testing later. Simple Contact component below.
|
148 | <pre>
|
149 | function Contact(props){
|
150 | var { name, onClick } = props;
|
151 | return (
|
152 | <div className="contact--default" onClick={onClick}>
|
153 | <span>{name}</span>
|
154 | </div>;
|
155 | )
|
156 | } </pre>
|
157 | <p class="noIntend">Don't worry if you not understanding this example. I will explain later</p>
|
158 |
|
159 | <h3>What is Container?</h3>
|
160 | <p>Container is also component. But It's main responsibility to fetch data using api and maintaining state and pass the state to it's children. Creating container component is similar like component. It also has react lifecycle method</p>
|
161 | <pre>
|
162 | class ContactListContainer extends React.Component{
|
163 | constructor(props){
|
164 | super(props);
|
165 | this.state={
|
166 | contacts:[]
|
167 | }
|
168 | }
|
169 | render(){
|
170 | return <div>
|
171 | {
|
172 | this.state.contacts.map((contact)=>{
|
173 | return <Contact name={contact.name} onClick={()=>{console.log(contact.id)} />
|
174 | })
|
175 | }
|
176 | </div>
|
177 | }
|
178 | }
|
179 | componentDidMount(){
|
180 | fetch('/api/v1/contacts').then(res=>res.json()).then(res)=>{
|
181 | this.setState({contacts:res})
|
182 | })
|
183 | }
|
184 | }
|
185 |
|
186 | </pre>
|
187 | <p class="noIntend">Don't worry if you not understanding this example. I will explain later</p>
|
188 |
|
189 | <h3>What is component documentation?</h3>
|
190 | <p> Once components grow, It is hard to find out available component and it is possible UI state of the component.
|
191 | So You need a documentation for your components. Each component has docs folder and you can define different UI state of your component.
|
192 | This will display your component documentation</p>
|
193 |
|
194 | <h3>How can i view my components as documentation?</h3>
|
195 | <p>You can run the below command to start your document server. It runs in the default port 9292.
|
196 | You may change your running port using --server:port={port} </p>
|
197 | <pre>npm run docs --server:componentPath=./src/components/</pre>
|
198 |
|
199 | <h3>What is it means --server:componentPath?</h3>
|
200 | <p>
|
201 | It means, You can mention your components library location in your application folder.
|
202 | Default location under src/components
|
203 | </p>
|
204 |
|
205 | <h3>Should i maintain component library inside application repository?</h3>
|
206 | <p> No. Not necessary. You may create separate repository for your component library.
|
207 | You can publish your component library and use inside your application</p>
|
208 |
|
209 | <h3>How to create separate component library?</h3>
|
210 | <p>You can create library using below component</p>
|
211 | <pre>fz-react-cli library <libraryname></pre>
|
212 | <div>open package.json file and add below code into scripts </div>
|
213 | <pre>"scripts":{
|
214 | "docs":"fz-react-cli docs",
|
215 | ...
|
216 | }</pre>
|
217 |
|
218 |
|
219 | <h3>What about folder structure of component library?</h3>
|
220 | <p>It is similar to app folder structure. You can create your component directly under src folder.
|
221 | No need to create under components folder. Remove some unwanted folder like container, app folder,etc.
|
222 | see below for sample
|
223 | </P>
|
224 | <pre>
|
225 | -<componentLibrary>
|
226 | -src
|
227 | -<componentnameasfoldername>
|
228 | -__tests__
|
229 | * <componentname>.spec.js
|
230 | -docs
|
231 | * <componentname>__<state1>.docs.js
|
232 | * <componentname>__<state2>.docs.js
|
233 | * <componentname>.js
|
234 | * <componentname>.css
|
235 |
|
236 | * index.js ( component export )
|
237 | * docs.js ( docs export )
|
238 | -docs (optional)
|
239 | * all.html (optional)
|
240 | * package.json
|
241 | * .gitignore
|
242 | * README.md
|
243 |
|
244 | Note:
|
245 | - folder
|
246 | * file
|
247 | -<> userfolder
|
248 | *<> userfile
|
249 | </pre>
|
250 |
|
251 | <h3>How to build my component library?</h3>
|
252 | <p>You can build your component library using below command. It builds your component library into different module system common js and
|
253 | es6. Understand different module system (AMD,Commonjs,umd) (<a href="https://www.davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/">link</a>) </p>
|
254 | <pre>npm run build:component
|
255 | npm run build:es
|
256 | </pre>
|
257 |
|
258 | <h3>Can I get single js for my component library?(UMD)</h3>
|
259 | <p>You may build as single js. but I am not prefer becuase component library has images and fonts and css and other resources.
|
260 | Build support bundle css inside js. there is no problem it is plain Css. If it use images fonts then we need to provide
|
261 | proper domain to download images and fonts. My opinion is to install as a dependencies and require you app and use it.
|
262 | </p>
|
263 |
|
264 | <h3>How to publish my component library?</h3>
|
265 | <p>It is out of the scope of this library. It is based on your registry configuration.
|
266 | If you configure npm registry using below component to use publish.
|
267 | you can get more information from this video tutorial (<a href="https://egghead.io/lessons/javascript-publishing-to-npm" target="_blank">Link</a>)</p>
|
268 | <pre>npm publish</pre>
|
269 |
|
270 | <h3>How to create other than component library (like redux) only js not css/images/fonts ?</h3>
|
271 | <p> you can use the same command. Update your package json information. </p>
|
272 | <pre>fz-react-cli library <libraryname></pre>
|
273 |
|
274 | <h3>Can i get single js for my library?(UMD file)</h3>
|
275 | <p>Yes. You can get your library as single umd js file. You can use the below command to create your umd js.
|
276 | Once build success, It is created under dist folder. you can also build your library in commonjs and es6 modules</p>
|
277 | <pre>npm run build:umd</pre>
|
278 | <pre>npm run build:component
|
279 | npm run build:es
|
280 | </pre>
|
281 |
|
282 | <h3>How can i change my umd global variable?</h3>
|
283 | <p>In package json file, you can see the variable "umdVar". You can change your globally export variable there.</p>
|
284 |
|
285 |
|
286 |
|
287 | <pre>fz-react-cli propertyToJson <propertyfolder> <jsonfolder> <defaultpropertyfile> <usedpropertyasfile>
|
288 | Example
|
289 | fz-react-cli propertyToJson ./support/properties/ ./support/i18n ./support/properties/ApplicationResources_en_US.properties ./support/properties/JSResources.properties
|
290 | </pre>
|
291 |
|
292 | <h3>Some restriction</h3>
|
293 | <p>don't create docs app name</p>
|
294 |
|
295 | <h3>Advanced</h3>
|
296 | <h3>Node Cluster Monitor</h3>
|
297 | <pre>fz-react-cli cluster:monitor</pre>
|
298 | <p>cluster config object </p>
|
299 | <pre>
|
300 | {
|
301 | "cluster":[
|
302 | {
|
303 | "ip": "localhost",
|
304 | "port": "4040"
|
305 | },
|
306 | {
|
307 | "ip": "localhost",
|
308 | "port": "4050"
|
309 | }
|
310 | ]
|
311 | }
|
312 |
|
313 | </pre>
|
314 |
|
315 | <h3>Node</h3>
|
316 | <pre>fz-react-cli node <host> <port> <repourl> <branch> </pre>
|
317 |
|
318 |
|
319 | <h3>Learn</h3>
|
320 | <ul>
|
321 | <li>ES2015/ES6</li>
|
322 | <li>React</li>
|
323 | <li>Redux</li>
|
324 | <li>React-Redux</li>
|
325 | <li>simple-normalizer</li>
|
326 | <li>redux-router-middle</li>
|
327 | <li>fz-i18n</li>
|
328 | <li>fz-propertytojson</li>
|
329 | <li>fz-dnd</li>
|
330 | <li>fz-permission</li>
|
331 | <li>fz-layout</li>
|
332 | <li>css-modules</li>
|
333 | <li>webpack</li>
|
334 | <li>Server Side Rendering</li>
|
335 | </ul>
|
336 | <p>Learn all topic in deep. I will explain basic in the following section</p>
|
337 | </div>
|
338 | <div id="basic" class="container-fluid">
|
339 | <h1>Basic</h1>
|
340 | <p>Before going to write application we are going to learn some basic. Those things only going to use most of the places.
|
341 | <h2>Javascript</h2>
|
342 | <P>In javascript, We are going to see following methods
|
343 | <ul>
|
344 | <li>Object.assign</li>
|
345 | <li>Object.keys methods</li>
|
346 | <li>Array.map</li>
|
347 | <li>Array.filter</li>
|
348 | <li>Array.reduce</li>
|
349 | <li>Function.apply</li>
|
350 | <li>Function.call</li>
|
351 | <li>Function.bind</li>
|
352 | </ul>
|
353 | After that ES2015 feature
|
354 | <ul>
|
355 | <li>arrow function</li>
|
356 | <li>class </li>
|
357 | <li>string template</li>
|
358 | <li>Promise</li>
|
359 | </ul>
|
360 | </P>
|
361 |
|
362 | <h3>Object.assign</h3>
|
363 | <pre>
|
364 | var target = Object.assign(target,source1,source2,...);</pre>
|
365 | <p>The Object.assign() method is used to copy the values of all properties from source objects to a target object. It will return the target object.</p>
|
366 | <iframe width="560" height="315" src="https://www.youtube.com/embed/aw7NfYhR5rc" frameborder="0" allowfullscreen></iframe>
|
367 |
|
368 | <h3>Array methods</h3>
|
369 | <h4>Map</h4>
|
370 | <p>Map function used to map value to correponding array element</p>
|
371 | <pre>
|
372 | var a1=[1,2,3];
|
373 | var a2=a1.map(function(val){
|
374 | return val*2;
|
375 | })
|
376 | a1=[1,2,3] a2=[2,4,6]
|
377 | </pre>
|
378 | <h4>Filter</h4>
|
379 | <p>Filter function used to filter values from give array</p>
|
380 | <pre>
|
381 | var a1=[1,2,3];
|
382 | var a2=a1.filter(function(val){
|
383 | val%2;
|
384 | })
|
385 | a1=[1,2,3] a2=[1,3]
|
386 | </pre>
|
387 | <h4>Reduce</h4>
|
388 | <p>Reduce function used to reduce one dimention array to single value.</p>
|
389 | <pre>
|
390 | var a1=[1,2,3];
|
391 | var a2=a1.reduce(function(result,next){
|
392 | return result+next;
|
393 | },0)
|
394 | a1=[1,2,3] a2=6
|
395 | </pre>
|
396 | <p> Try these examples
|
397 | <a href="http://reactivex.io/learnrx/">Link</a>
|
398 | </p>
|
399 |
|
400 | <h1>ES2015/ES6</h1>
|
401 | <h3>Arrow Function</h3>
|
402 | <pre>
|
403 | function test(a,b){
|
404 | return a+b
|
405 | }
|
406 |
|
407 | var test = (a,b) => {
|
408 | return a+b
|
409 | }
|
410 |
|
411 | only return directly return no need curly braces
|
412 | var test = (a,b) => a+b
|
413 |
|
414 | Single argument no need function of parentheses
|
415 | var test = a => a+10;
|
416 |
|
417 | function test(){
|
418 | return {
|
419 | a:1
|
420 | }
|
421 | }
|
422 |
|
423 | object return using arrow function
|
424 |
|
425 | var test = () => ({a:1})
|
426 | </pre>
|
427 | <h3>Class</h3>
|
428 | <pre>
|
429 | class Test{
|
430 |
|
431 | }
|
432 |
|
433 | class Test{
|
434 | constructor(){
|
435 | super();
|
436 | }
|
437 | }
|
438 | class Test{
|
439 | constructor(){
|
440 |
|
441 | }
|
442 | get(){
|
443 | //instance method
|
444 | }
|
445 | }
|
446 | Test.method=function(){
|
447 | //static method
|
448 | }
|
449 | Test.obj={}
|
450 | //Static member
|
451 |
|
452 | </pre>
|
453 | <h3>Template string</h3>
|
454 | <pre>
|
455 | var str1="Hello"
|
456 | var str2="World"
|
457 | var newString=str1+", "+str2;
|
458 |
|
459 | var newString =`${str1}, ${str2}`
|
460 | </pre>
|
461 | <h3>Promise</h3>
|
462 | <pre>
|
463 | Callback hell...
|
464 | function test(cb){
|
465 | code...
|
466 | may throw error
|
467 | cb();
|
468 |
|
469 | }
|
470 | function test1(cb){
|
471 | code...
|
472 | may throw error
|
473 | test()
|
474 | }
|
475 |
|
476 | horizontal Flattern callback hell using promise
|
477 |
|
478 | var promise =new Promise((resolve,reject)=>{
|
479 | task...
|
480 | taskSuccess call resolve()
|
481 | taskFailure call reject
|
482 |
|
483 | })
|
484 | promise.then(()=>{
|
485 | return "pass next then"
|
486 | },(e)=>{
|
487 | //error handling
|
488 | }).then((prePass)=>{
|
489 | return
|
490 | }).catch((e)=>{
|
491 | error handling...
|
492 | })
|
493 |
|
494 | Vertical flattern/Normal flow using async function
|
495 | async function feature comes in ES7
|
496 |
|
497 | Some of the feature not supported all browser. We are using babel to transpile es2015 code into es5 code. We will see babel in feature
|
498 | </pre>
|
499 | <h3>Functional Programming</h3>
|
500 | <p><a href="https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch1.html">book</a></p>
|
501 | <h3>Pure Function</h3>
|
502 | <h4>What is pure function?</h4>
|
503 | <p>A pure function is a function which given the same input, will always return the same output and also not produces any side effects.</p>
|
504 | <pre>
|
505 | function add(a,b){
|
506 | return a+b;
|
507 | }
|
508 | </pre>
|
509 |
|
510 | <pre>
|
511 | Not pure function
|
512 | function add(){
|
513 | var a=document.getElementById("a").value;//access dom object
|
514 | var b=document.getElementById("b").value;
|
515 | return a+b;
|
516 | }
|
517 | function add(a,b){
|
518 | updateIntoDisk(a+b);
|
519 | return a+b;
|
520 | }
|
521 | function add(obj){
|
522 | obj.c=obj.a+obj.b;
|
523 | return obj;
|
524 | }
|
525 | </pre>
|
526 | <h4>What is side effect?</h4>
|
527 | <p>modification of some kind of state</p>
|
528 |
|
529 | <h4>Advantages of purefunction</h4>
|
530 | <p>
|
531 | Code easy to testable. easy to debug.
|
532 | </p>
|
533 | </div>
|
534 | <div id="react" class="container-fluid">
|
535 | <h1>React </h1>
|
536 |
|
537 | <h3>JSX</h3>
|
538 |
|
539 | <ol>
|
540 | <li>
|
541 | How to create element
|
542 | <pre>
|
543 | <script type="text/plain">
|
544 | var element=<div id="test">test</div>
|
545 |
|
546 | equalenent js
|
547 |
|
548 | React.createElement('div',{id:"test"},"test");
|
549 | </script>
|
550 | </pre>
|
551 | </li>
|
552 | <li>
|
553 | JSX Condition
|
554 | <pre>
|
555 | <script type="text/plain">
|
556 | var element=(<div>
|
557 | {
|
558 | a==true ? <div>A</div> : <div>Else</div>
|
559 | }
|
560 | </div>)
|
561 | </script>
|
562 | </pre>
|
563 | </li>
|
564 | <li>
|
565 | JSX Loop
|
566 | <pre>
|
567 | <script type="text/plain">
|
568 | var arr = [1, 2, 3];
|
569 | var element = (
|
570 | <div>
|
571 | {arr.map(no => {
|
572 | <div>{no}</div>;
|
573 | })
|
574 | }
|
575 | </div>
|
576 | );
|
577 | </script>
|
578 | </pre>
|
579 | </li>
|
580 | <li>
|
581 | How to create functional Component
|
582 | <pre>
|
583 | <script type="text/plain">
|
584 | var Text=(props)=>{
|
585 | <div>Text</div>
|
586 | }
|
587 | </script>
|
588 | </pre>
|
589 | </li>
|
590 | <li>
|
591 | How to create Stateless es6 Component
|
592 | <pre>
|
593 | <script type="text/plain">
|
594 | class Text extends React.Component {
|
595 | render() {
|
596 | <div>Text</div>;
|
597 | }
|
598 | }
|
599 | </script>
|
600 | </pre>
|
601 | </li>
|
602 |
|
603 | <li>
|
604 | How to create Stateful es6 Component
|
605 | <pre>
|
606 | <script type="text/plain">
|
607 | class Text extends React.Component {
|
608 | constructor(props) {
|
609 | super(props);
|
610 | this.state = {
|
611 | count: 0
|
612 | };
|
613 | }
|
614 | render() {
|
615 | <div>Text {this.state.count}</div>;
|
616 | }
|
617 | }
|
618 | </script>
|
619 | </pre>
|
620 | </li>
|
621 | <li>
|
622 | Component Lifecycle
|
623 | <pre>
|
624 | <script type="text/plain">
|
625 | class Text extends React.Component {
|
626 | constructor(props) {
|
627 | super(props);
|
628 | this.state = {
|
629 | count: 0
|
630 | };
|
631 | }
|
632 | componentWillMount() {}
|
633 | componentWillReceiveProps(nextProps) {}
|
634 | componentWillUpdate() {}
|
635 | componentWillUnmount() {}
|
636 | render() {
|
637 | <div>Text {this.state.count}</div>;
|
638 | }
|
639 | componentDidMount() {}
|
640 | componentDidUpdate(preProps, preState) {}
|
641 | shouldComponentUpdate(nextProps, nextState) {}
|
642 | }
|
643 | </script>
|
644 | </pre>
|
645 | </li>
|
646 | <li>
|
647 | Note Important
|
648 | <pre>
|
649 | <script type="text/plain">
|
650 | 1. Component Name Caps
|
651 | 2. Parent to child should pass as props
|
652 | 3. Child to parent data should pass throw callback method. do not pass data throw event. It is hard to track and some one can
|
653 | 4.
|
654 | </script>
|
655 | </pre>
|
656 | </li>
|
657 | </ol>
|
658 | <a href="facebook.github.io/react/">More</a>
|
659 | </div>
|
660 | <div id="redux" class="container-fluid">
|
661 | <h1>Redux</h1>
|
662 |
|
663 | <p>Redux is a state management library. You can get more information from redux <a href="http://redux.js.org/docs/introduction/">docs</a>. Here we going to discuss about basic idea of state management</p>
|
664 | <h4>What is State?</h4>
|
665 | <p>State means data and It can be changed based on action/time,etc.<br />
|
666 | Example switch has two state one is "ON" another one is "OFF".
|
667 | You can "ON" or "OFF" a switch based on action.
|
668 | </p>
|
669 | <img src="./img/switch.jpg" height="200" width="200"/>
|
670 | <pre>
|
671 | function switch(state="OFF"){
|
672 | return state;
|
673 | }
|
674 | switch ();
|
675 | </pre>
|
676 | <p>
|
677 | I just define pure function. It is going to handle switch state.
|
678 | If you call switch function, you get the switch state.
|
679 | Now it is always return "OFF" state. because we didn't handle any action. <br />My action is going to press switch button.
|
680 | We slightly modify above function which handle our actions
|
681 | </p>
|
682 | <pre>
|
683 | function switch(state="OFF",action){
|
684 | if(action=="PRESS"){
|
685 | if(state=="ON"){
|
686 | state="OFF";
|
687 | }
|
688 | else{
|
689 | state="ON";
|
690 | }
|
691 | }
|
692 | return state;
|
693 | }
|
694 |
|
695 | var previousSwitchState="ON";
|
696 | var currentSwitchState= switch(previousSwitchState, "PRESS");
|
697 | console.log(currentSwitchState)
|
698 | </pre>
|
699 | <p>Anybody feels problem in the above action. <br />Some problem in the above action.
|
700 | Assume switch is already "ON" state I press switch for "ON". But It toggle it's state.
|
701 | So action and some more information needed. So instead of action as string we pass it as object.
|
702 | </p>
|
703 | <p>
|
704 | {type:"ACTION_TYPE",data:{information about action}}
|
705 | </p>
|
706 | <pre>
|
707 | var Off = {type:"PRESS",data:"up"}
|
708 | var On = {type:"PRESS",data:"down"}
|
709 |
|
710 | function switch(state="OFF",action){
|
711 | if(action.type=="PRESS"){
|
712 | if(action.data =="up"){
|
713 | state="OFF";
|
714 | }
|
715 | else if(action.data =="down"){
|
716 | state="ON";
|
717 | }
|
718 | }
|
719 | return state;
|
720 | }
|
721 | var state1 =switch(previousState,on);
|
722 | var state2 = switch(state1,off);
|
723 | </pre>
|
724 |
|
725 |
|
726 | <p>Switch was connected to bulb. Whenever switch state changes bulb going to glow or not glow.
|
727 | </p>
|
728 | <pre>
|
729 | bulb(switchState){
|
730 | if(switchState=="ON"){
|
731 | return "glow"
|
732 | }
|
733 | else{
|
734 | return "not glow"
|
735 | }
|
736 | }
|
737 | var previousState;
|
738 | dispatch(action){
|
739 | var nextState=switch(previousState,action);
|
740 | console.log(bulb(nextState));
|
741 | previousState=nextState;
|
742 | return action;
|
743 | }
|
744 |
|
745 | var Off = {type:"PRESS",data:"up"}
|
746 | var On = {type:"PRESS",data:"down"}
|
747 |
|
748 | dispatch(On);
|
749 | dispatch(Off);
|
750 |
|
751 | </pre>
|
752 |
|
753 |
|
754 |
|
755 |
|
756 | <p>state data nothing but your api data. switch and bulb are your html when ever data changes you have to update the UI</p>
|
757 | </div>
|
758 | <div id="router" class="container-fluid">
|
759 | Redux router middleware
|
760 | </div>
|
761 | <div id="serverside" class="container-fluid">
|
762 | <h1>Server side rendering</h1>
|
763 | <p> Notice:
|
764 | 1. Not access document/window/global variable inside render method
|
765 | 2. * browser time zone string (ip based)
|
766 | 4. xmlhttprequest - both support node, browser library
|
767 | 5. Initial data portalInfo hard coded (change to api call)
|
768 | 6. * editor js loading handling
|
769 | 7. build deploy and rollback. node server start and stop
|
770 | 8. node build update
|
771 | 9. flow of request server side rendering
|
772 | Header customization
|
773 | 1. request->tomcat filter -> jsp -> http connect to node (if auth cookie) -> (do all api calls with or without cookie)-> generate html using that state and return to jsp -> jsp append html response -> client response
|
774 | 2. request -> node -> call apis with request cookies -> return html -> client response
|
775 | 10. threshold
|
776 |
|
777 | Advantage
|
778 | reduce api call round trip
|
779 | fast rendering (html once parse done render)
|
780 | SEO easy to parse (no need client rendering from seo)
|
781 |
|
782 |
|
783 | server side rendering css modules.
|
784 | server side rendering code splitting
|
785 | https://github.com/kriasoft/isomorphic-style-loader
|
786 | https://github.com/lyft/universal-async-component
|
787 | hash changing ={
|
788 |
|
789 | }
|
790 | Node cluster Manager
|
791 | git:””,hash:”"
|
792 | node:[{“ip”,”port”},{“ip”,”port”}];
|
793 | node information - {ip:””, port:””}
|
794 | port free?
|
795 | git clone succes?
|
796 | git hard clone
|
797 | List of Cluster
|
798 | node verification
|
799 | https://stackoverflow.com/questions/16525617/how-to-detect-users-timezone/16526897#16526897
|
800 | https://github.com/aickin/react-dom-stream
|
801 | https://medium.com/@aickin/whats-new-with-server-side-rendering-in-react-16-9b0d78585d67
|
802 | </p>
|
803 | </div>
|
804 |
|
805 |
|
806 |
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 |
|
814 |
|
815 | </body>
|
816 | </html>
|