SlideShare a Scribd company logo
ReactJS
The future of web development
Thanh Tuong | ReactJS | 2016
1
Thanh Tuong | ReactJS | 2016
2
https://github.com/facebook/react/wiki/Sites-Using-React
History
 In 2010, Facebook released an extension for PHP called XHP.
 XHP help to decrease XSS attack and make front-end much both readable
and understand.
Thanh Tuong | ReactJS | 2016
<?php
if ($_POST['name']) {
echo <span>Hello,
{$_POST['name']}</span>;
} else {
echo
<form method="post">
What is your name?<br />
<input type="text" name="name" />
<input type="submit" />
</form>;
}
<?php
if ($_POST['name']) {
?>
<span>Hello, <?=$_POST['name']?>.</span>
<?php
} else {
?>
<form method="post">
What is your name?<br>
<input type="text" name="name">
<input type="submit">
</form>
<?php
}
PHP XHP
3
History (cont)
 But…
 There was a distinct problem with XHP: dynamic web applications require
many roundtrips to the server.
 XHP did not solve this problem.
 A Facebook engineer negotiated with his manager to take XHP into the
browser using JavaScript and was granted six months to try it.
 And…
Thanh Tuong | ReactJS | 2016
4
React was
born
ReactJS {purpose}
 Creating user interface(V in MVC model).
 Building large applications with data that changes over time.
Thanh Tuong | ReactJS | 2016
5
var React = React.createClass({
render: function() {
return (
<h1> Hello React </h1>
);
}
});
ReactDOM.render(<React />,document.getElementById('container'));
Syntax
ReactJS {contents}
 JSX
 Virtual-DOM
 Props
 PropTypes
 State
 Refs
 LifeCycle
 Flux Architech
 Thinking in React
 Routing
Thanh Tuong | ReactJS | 2016
6
ReactJS {contents}
 JSX
 Virtual-DOM
 Props
 PropTypes
 State
 Refs
 LifeCycle
 Flux Architech
 Thinking in React
 Routing
Thanh Tuong | ReactJS | 2016
7
ReactJS {JSX}
 JSX is a JavaScript syntax extension that looks similar to XML.
 Concise and familiar syntax for defining tree structures with attributes.
 Make large trees easier to read than function calls or object literals.
 Can be used in both HTML tags and Components.
Thanh Tuong | ReactJS | 2016
8
ReactJS {JSX – examples}
 HTML tags
 var myDivElement = <div className="foo" > HTML tags </div>;
 ReactDOM.render(myDivElement, document.getElementById('example'));
 Component
 var MyComponent = React.createClass({/*...*/});
 var myElement = <MyComponent />;
 ReactDOM.render(myElement, document.getElementById('example'));
Thanh Tuong | ReactJS | 2016
9
ReactJS {JSX – examples (cont)}
 HTML tags (without JSX)
 var myDivElement = React.createElement('div', {className: 'foo'}, 'HTML tags');
 ReactDOM.render(myDivElement, document.getElementById('example'));
 Component (without JSX)
var MyComponent = React.createClass({
render: function() {
return (
React.createElement('h1',{}, 'Component without JSX')
);
}
});
var myElement = <MyComponent />;
ReactDOM.render(myElement, document.getElementById('content'));
Thanh Tuong | ReactJS | 2016
10
ReactJS {JSX – Transform}
 React JSX transforms from an XML-like syntax into native JavaScript.
 XML elements, attributes and children are transformed into arguments that
are passed to React.createElement.
Thanh Tuong | ReactJS | 2016
11
Children
ReactJS {JSX –Namespaced}
 What if you are building a component with many children? For example
Form.
 Namespaced components help to make component simpler and easier.
 You just need to create your "sub-components" as attributes of the main
component.
Thanh Tuong | ReactJS | 2016
12
ReactJS {JSX –Namespaced (cont)}
Thanh Tuong | ReactJS | 2016
13
ReactJS {contents}
 JSX
 Virtual-DOM
 Props
 PropTypes
 State
 Refs
 LifeCycle
 Flux Architech
 Thinking in React
 Routing
Thanh Tuong | ReactJS | 2016
14
ReactJS {Virtual-DOM}
 Problem:
 DOM manipulation is expensive.
 Re-render all parts of DOM make your app slowly.
 When the component’s state is changed, React will compare with DOM
element to make smallest change.
 Is made by React.createElement().
 https://www.youtube.com/watch?v=BYbgopx44vo
Thanh Tuong | ReactJS | 2016
15
ReactJS {Virtual-DOM}
Thanh Tuong | ReactJS | 2016
16
Only diff changes
from the two V-DOMs
are applied to real
DOM
ReactJS {Virtual-DOM (cont)}
 1. Backbone.js recontruct
DOM elements marked as
“change”.
 2. Backbone.js recontruct
All DOM elements.
 3. ReactJS recontruct DOM
elements base on calculate the
difference.
Thanh Tuong | ReactJS | 2016
17
ReactJS {contents}
 JSX
 Virtual-DOM
 Props
 PropTypes
 State
 Refs
 LifeCycle
 Flux Architech
 Thinking in React
 Routing
Thanh Tuong | ReactJS | 2016
18
ReactJS {props}
 Used to pass parameter from parent to children.
 var HelloReact = React.createClass({
render: function() {
return (
<h1> Hello, {this.props.name} </h1>
);
}
});
ReactDOM.render(<HelloReact name="ReactJS!!!" />, node);
Thanh Tuong | ReactJS | 2016
19
ReactJS {contents}
 JSX
 Virtual-DOM
 Props
 PropTypes
 State
 Refs
 LifeCycle
 Flux Architech
 Thinking in React
 Routing
Thanh Tuong | ReactJS | 2016
20
ReactJS {PropTypes}
 For validate the prop’s value input.
 var HelloReact = React.createClass({
propTypes: {
name: React.PropTypes.number
},
render: function() {
return (
<h1> Hello, {this.props.name} </h1>
);
}
});
ReactDOM.render(<HelloReact name="thanh" />, document.getElementById('content'));
Thanh Tuong | ReactJS | 2016
21
ReactJS {contents}
 JSX
 Virtual-DOM
 Props
 PropTypes
 State
 Refs
 LifeCycle
 Flux Architech
 Thinking in React
 Routing
Thanh Tuong | ReactJS | 2016
22
ReactJS {state}
 To manage state inside component.
 getInitialState() function: init value for variable.
 setState() function: update new value for variable.
Thanh Tuong | ReactJS | 2016
23
ReactJS {state-(cont)}
 When you should use state?
 Respond to user input.
 Server request.
 or the passage of time.
Thanh Tuong | ReactJS | 2016
24
ReactJS { props vs state }
Features props state
Can get initial value from
parent Component?
Yes Yes
Can be changed by
parent Component?
Yes No
Can set default values
inside Component?
Yes Yes
Can change inside
Component?
No Yes
Can set initial value for
child Components?
Yes Yes
Can change in child
Components?
Yes No
Thanh Tuong | ReactJS | 2016
25
ReactJS {contents}
 JSX
 Virtual-DOM
 Props
 PropTypes
 State
 Refs
 LifeCycle
 Flux Architech
 Thinking in React
 Routing
Thanh Tuong | ReactJS | 2016
26
ReactJS {refs}
 How we make focus to input element after clear data from input element?
 How we can make a search with many criteria ?
 …
Thanh Tuong | ReactJS | 2016
27
Refs
ReactJS {refs-(cont)}
Thanh Tuong | ReactJS | 2016
28
Type…
Click clear
ReactJS {contents}
 JSX
 Virtual-DOM
 Props
 PropTypes
 State
 Refs
 LifeCycle
 Flux Architech
 Thinking in React
 Routing
Thanh Tuong | ReactJS | 2016
29
ReactJS {LifeCycle}
 Each component has its own lifecycle events.
 Ex:
 If we wanted to make an ajax request on the initial render and fetch some data,
where would we do that?
 If we wanted to run some logic whenever our props changed, how would we do
that?
 …
Thanh Tuong | ReactJS | 2016
30
LifeCycle
events
ReactJS {LifeCycle (cont)}
 componentWillMount
 Invoked once (both on the client and server ) before the initial render.
 Good place to make connection to your db service (ex: firebase,...)
 Do not call set state method here.
Thanh Tuong | ReactJS | 2016
31
ReactJS {LifeCycle (cont)}
 componentDidMount
 Invoked once, only on the client (not on the server).
 Immediately after the initial rendering occurs.
 It is good place for you to make AJAX request to fetch data for first used.
Thanh Tuong | ReactJS | 2016
32
ReactJS {LifeCycle (cont)}
 componentWillReceiveProps
 Invoked when a component is receiving new props.
 This method is not called for the initial render.
 Use this method as a way to react to a prop change before render() is called by
updating the state with setState.
Thanh Tuong | ReactJS | 2016
33
ReactJS {LifeCycle (cont)}
 componentWillUnmount
 Invoked immediately before a component is unmounted from the DOM.
 Perform any necessary cleanup in this method(Ex: invalidating timers, clear up
DOM elements were created at componentDidMount)
Thanh Tuong | ReactJS | 2016
34
Nothing!!!
http://facebook.github.io/react/docs/component-
specs.html
ReactJS {contents}
 JSX
 Virtual-DOM
 Props
 PropTypes
 State
 Refs
 LifeCycle
 Flux Architech
 Thinking in React
 Routing
Thanh Tuong | ReactJS | 2016
35
ReactJS {Flux}
 Flux is the application architecture.
 Making data changes easy.
 Remove the burden of having a component manage its own state.
 The data is moved to the central called Store.
 If your app doesn’t have and or care about dynamic data, Flux might not
be the best choice.
 Unidirectional data flow.
Thanh Tuong | ReactJS | 2016
36
ReactJS {Flux - flow}
Thanh Tuong | ReactJS | 2016
37
ReactJS {Flux - flow}
 Dispatcher
 Is the central hub that manages all data flow in a Flux application.
 Essentially a registry of callbacks into the stores.
Thanh Tuong | ReactJS | 2016
38
ReactJS {Flux - flow}
Dispatcher
Store Store Store Store
Thanh Tuong | ReactJS | 2016
39 When an action
creator provides
the dispatcher with
a new action, all
stores in the
application receive
the action via the
callbacks in the
registry.
Broadcast
ReactJS {Flux - flow}
 Stores
 Stores contain the application state and logic.
 Manage the state of many objects.
 Do not represent a single record of data like ORM models do.
 Store registers itself with the dispatcher and provides it with a callback.
Thanh Tuong | ReactJS | 2016
40
ReactJS {Flux - flow}
 Views
 Typical React component.
 After is mounted, it goes and get its initial state from Store and setup listener.
 When it receives the event from the store, it first requests the new data it needs
via the stores' public getter methods.
 Then, it calls its own setState() method, causing its render() method and the
render() method of all its descendants to run.
Thanh Tuong | ReactJS | 2016
41
ReactJS {Flux - flow}
Thanh Tuong | ReactJS | 2016
42
ReactJS {Flux - Implement}
 Flux is just an architect. So, you can design new framework by yourself base
on this architect.
 Many JavaScript libraries help you implement flux like:
 Flux (by Facebook: https://github.com/facebook/flux)
 Reflux(by Mikael Brassman: https://github.com/reflux/refluxjs)
 Redux(by Dan Abramov: https://github.com/reactjs/redux)
 …
Thanh Tuong | ReactJS | 2016
43
ReactJS {Flux – source code}
 https://github.com/tylermcginnis/Flux-Todolist
Thanh Tuong | ReactJS | 2016
44
ReactJS {contents}
 JSX
 Virtual-DOM
 Props
 PropTypes
 State
 Refs
 LifeCycle
 Flux Architech
 Routing
Thanh Tuong | ReactJS | 2016
45
ReactJS { thinking in… }
Thanh Tuong | ReactJS | 2016
46
How many
components
should I have?
How can I
break it?
ReactJS { thinking in… }
Thanh Tuong | ReactJS | 2016
47
1
4
2
2
3
3
5
5 FilterableProductTable: contains the entirety of the example
5 1 4SearchBar: receives all user input
4 ProductTable: displays and filters the data collection based on user input
2 ProductCategoryRow: displays a heading for each category
3 ProductRow: displays a row for each product
ReactJS { thinking in… }
Thanh Tuong | ReactJS | 2016
48
ReactJS {Routing}
 Make UI consistent with URL.
 https://github.com/reactjs/react-router/blob/latest/docs.
Thanh Tuong | ReactJS | 2016
49
ReactJS {references}
 http://tylermcginnis.com/reactjs-tutorial-a-comprehensive-guide-to-
building-apps-with-react/
 https://facebook.github.io/react/docs/getting-started.html
 https://github.com/reactjs/react-router/tree/latest/docs
 http://teropa.info/blog/2015/03/02/change-and-its-detection-in-javascript-
frameworks.html
 https://www.airpair.com/angularjs/posts/angular-vs-react-the-tie-breaker
Thanh Tuong | ReactJS | 2016
50
@ThanhTuong
SE at KMS technology
Thanh Tuong | ReactJS | 2016
51

More Related Content

PPTX
[Final] ReactJS presentation
PPTX
Introduction to React JS
PPTX
React workshop
PPTX
Introduction to React JS for beginners | Namespace IT
PPTX
ReactJS presentation.pptx
PPTX
React JS - A quick introduction tutorial
PPTX
Reactjs
PDF
Introduction to ReactJS
[Final] ReactJS presentation
Introduction to React JS
React workshop
Introduction to React JS for beginners | Namespace IT
ReactJS presentation.pptx
React JS - A quick introduction tutorial
Reactjs
Introduction to ReactJS

What's hot (20)

PPTX
Introduction to React JS for beginners
PDF
React js
PPTX
React js programming concept
PPTX
Its time to React.js
PDF
Introduction to React JS
PPTX
Introduction to React
PPTX
React JS: A Secret Preview
PPTX
reactJS
PPTX
ODP
Introduction to ReactJS
PPTX
Introduction to react_js
PPTX
React + Redux Introduction
PPTX
Intro to React
PPTX
React hooks
PDF
React JS - Introduction
PPTX
React workshop presentation
PPT
React js
PPTX
React js
Introduction to React JS for beginners
React js
React js programming concept
Its time to React.js
Introduction to React JS
Introduction to React
React JS: A Secret Preview
reactJS
Introduction to ReactJS
Introduction to react_js
React + Redux Introduction
Intro to React
React hooks
React JS - Introduction
React workshop presentation
React js
React js
Ad

Similar to ReactJS presentation (20)

PDF
React mit TypeScript – eine glückliche Ehe
PPTX
Introduction to react and redux
PDF
ReactJS - frontend web developing reactjs
PPTX
React - Start learning today
PPTX
GDSC NITS Presents Kickstart into ReactJS
PDF
React
PPTX
PDF
Server side rendering with React and Symfony
PDF
A full introductory guide to React
PDF
RicoAjaxEngine
PDF
RicoAjaxEngine
PDF
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
PDF
OSGi and Spring Data for simple (Web) Application Development
PDF
Universal JS Applications with React
PPTX
PDF
An introduction to React.js
PDF
Manage the Flux of your Web Application: Let's Redux
PPTX
ReactJS_First_Practical for the freshers.pptx
PPTX
Building user interface with react
PDF
Reactивная тяга
React mit TypeScript – eine glückliche Ehe
Introduction to react and redux
ReactJS - frontend web developing reactjs
React - Start learning today
GDSC NITS Presents Kickstart into ReactJS
React
Server side rendering with React and Symfony
A full introductory guide to React
RicoAjaxEngine
RicoAjaxEngine
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
OSGi and Spring Data for simple (Web) Application Development
Universal JS Applications with React
An introduction to React.js
Manage the Flux of your Web Application: Let's Redux
ReactJS_First_Practical for the freshers.pptx
Building user interface with react
Reactивная тяга
Ad

Recently uploaded (20)

PDF
Newfamily of error-correcting codes based on genetic algorithms
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
KodekX | Application Modernization Development
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
DevOps & Developer Experience Summer BBQ
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Event Presentation Google Cloud Next Extended 2025
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
Omni-Path Integration Expertise Offered by Nor-Tech
PPTX
Big Data Technologies - Introduction.pptx
Newfamily of error-correcting codes based on genetic algorithms
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Review of recent advances in non-invasive hemoglobin estimation
Understanding_Digital_Forensics_Presentation.pptx
MYSQL Presentation for SQL database connectivity
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
KodekX | Application Modernization Development
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
DevOps & Developer Experience Summer BBQ
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Smarter Business Operations Powered by IoT Remote Monitoring
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Event Presentation Google Cloud Next Extended 2025
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
Sensors and Actuators in IoT Systems using pdf
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Transforming Manufacturing operations through Intelligent Integrations
Omni-Path Integration Expertise Offered by Nor-Tech
Big Data Technologies - Introduction.pptx

ReactJS presentation

  • 1. ReactJS The future of web development Thanh Tuong | ReactJS | 2016 1
  • 2. Thanh Tuong | ReactJS | 2016 2 https://github.com/facebook/react/wiki/Sites-Using-React
  • 3. History  In 2010, Facebook released an extension for PHP called XHP.  XHP help to decrease XSS attack and make front-end much both readable and understand. Thanh Tuong | ReactJS | 2016 <?php if ($_POST['name']) { echo <span>Hello, {$_POST['name']}</span>; } else { echo <form method="post"> What is your name?<br /> <input type="text" name="name" /> <input type="submit" /> </form>; } <?php if ($_POST['name']) { ?> <span>Hello, <?=$_POST['name']?>.</span> <?php } else { ?> <form method="post"> What is your name?<br> <input type="text" name="name"> <input type="submit"> </form> <?php } PHP XHP 3
  • 4. History (cont)  But…  There was a distinct problem with XHP: dynamic web applications require many roundtrips to the server.  XHP did not solve this problem.  A Facebook engineer negotiated with his manager to take XHP into the browser using JavaScript and was granted six months to try it.  And… Thanh Tuong | ReactJS | 2016 4 React was born
  • 5. ReactJS {purpose}  Creating user interface(V in MVC model).  Building large applications with data that changes over time. Thanh Tuong | ReactJS | 2016 5 var React = React.createClass({ render: function() { return ( <h1> Hello React </h1> ); } }); ReactDOM.render(<React />,document.getElementById('container')); Syntax
  • 6. ReactJS {contents}  JSX  Virtual-DOM  Props  PropTypes  State  Refs  LifeCycle  Flux Architech  Thinking in React  Routing Thanh Tuong | ReactJS | 2016 6
  • 7. ReactJS {contents}  JSX  Virtual-DOM  Props  PropTypes  State  Refs  LifeCycle  Flux Architech  Thinking in React  Routing Thanh Tuong | ReactJS | 2016 7
  • 8. ReactJS {JSX}  JSX is a JavaScript syntax extension that looks similar to XML.  Concise and familiar syntax for defining tree structures with attributes.  Make large trees easier to read than function calls or object literals.  Can be used in both HTML tags and Components. Thanh Tuong | ReactJS | 2016 8
  • 9. ReactJS {JSX – examples}  HTML tags  var myDivElement = <div className="foo" > HTML tags </div>;  ReactDOM.render(myDivElement, document.getElementById('example'));  Component  var MyComponent = React.createClass({/*...*/});  var myElement = <MyComponent />;  ReactDOM.render(myElement, document.getElementById('example')); Thanh Tuong | ReactJS | 2016 9
  • 10. ReactJS {JSX – examples (cont)}  HTML tags (without JSX)  var myDivElement = React.createElement('div', {className: 'foo'}, 'HTML tags');  ReactDOM.render(myDivElement, document.getElementById('example'));  Component (without JSX) var MyComponent = React.createClass({ render: function() { return ( React.createElement('h1',{}, 'Component without JSX') ); } }); var myElement = <MyComponent />; ReactDOM.render(myElement, document.getElementById('content')); Thanh Tuong | ReactJS | 2016 10
  • 11. ReactJS {JSX – Transform}  React JSX transforms from an XML-like syntax into native JavaScript.  XML elements, attributes and children are transformed into arguments that are passed to React.createElement. Thanh Tuong | ReactJS | 2016 11 Children
  • 12. ReactJS {JSX –Namespaced}  What if you are building a component with many children? For example Form.  Namespaced components help to make component simpler and easier.  You just need to create your "sub-components" as attributes of the main component. Thanh Tuong | ReactJS | 2016 12
  • 13. ReactJS {JSX –Namespaced (cont)} Thanh Tuong | ReactJS | 2016 13
  • 14. ReactJS {contents}  JSX  Virtual-DOM  Props  PropTypes  State  Refs  LifeCycle  Flux Architech  Thinking in React  Routing Thanh Tuong | ReactJS | 2016 14
  • 15. ReactJS {Virtual-DOM}  Problem:  DOM manipulation is expensive.  Re-render all parts of DOM make your app slowly.  When the component’s state is changed, React will compare with DOM element to make smallest change.  Is made by React.createElement().  https://www.youtube.com/watch?v=BYbgopx44vo Thanh Tuong | ReactJS | 2016 15
  • 16. ReactJS {Virtual-DOM} Thanh Tuong | ReactJS | 2016 16 Only diff changes from the two V-DOMs are applied to real DOM
  • 17. ReactJS {Virtual-DOM (cont)}  1. Backbone.js recontruct DOM elements marked as “change”.  2. Backbone.js recontruct All DOM elements.  3. ReactJS recontruct DOM elements base on calculate the difference. Thanh Tuong | ReactJS | 2016 17
  • 18. ReactJS {contents}  JSX  Virtual-DOM  Props  PropTypes  State  Refs  LifeCycle  Flux Architech  Thinking in React  Routing Thanh Tuong | ReactJS | 2016 18
  • 19. ReactJS {props}  Used to pass parameter from parent to children.  var HelloReact = React.createClass({ render: function() { return ( <h1> Hello, {this.props.name} </h1> ); } }); ReactDOM.render(<HelloReact name="ReactJS!!!" />, node); Thanh Tuong | ReactJS | 2016 19
  • 20. ReactJS {contents}  JSX  Virtual-DOM  Props  PropTypes  State  Refs  LifeCycle  Flux Architech  Thinking in React  Routing Thanh Tuong | ReactJS | 2016 20
  • 21. ReactJS {PropTypes}  For validate the prop’s value input.  var HelloReact = React.createClass({ propTypes: { name: React.PropTypes.number }, render: function() { return ( <h1> Hello, {this.props.name} </h1> ); } }); ReactDOM.render(<HelloReact name="thanh" />, document.getElementById('content')); Thanh Tuong | ReactJS | 2016 21
  • 22. ReactJS {contents}  JSX  Virtual-DOM  Props  PropTypes  State  Refs  LifeCycle  Flux Architech  Thinking in React  Routing Thanh Tuong | ReactJS | 2016 22
  • 23. ReactJS {state}  To manage state inside component.  getInitialState() function: init value for variable.  setState() function: update new value for variable. Thanh Tuong | ReactJS | 2016 23
  • 24. ReactJS {state-(cont)}  When you should use state?  Respond to user input.  Server request.  or the passage of time. Thanh Tuong | ReactJS | 2016 24
  • 25. ReactJS { props vs state } Features props state Can get initial value from parent Component? Yes Yes Can be changed by parent Component? Yes No Can set default values inside Component? Yes Yes Can change inside Component? No Yes Can set initial value for child Components? Yes Yes Can change in child Components? Yes No Thanh Tuong | ReactJS | 2016 25
  • 26. ReactJS {contents}  JSX  Virtual-DOM  Props  PropTypes  State  Refs  LifeCycle  Flux Architech  Thinking in React  Routing Thanh Tuong | ReactJS | 2016 26
  • 27. ReactJS {refs}  How we make focus to input element after clear data from input element?  How we can make a search with many criteria ?  … Thanh Tuong | ReactJS | 2016 27 Refs
  • 28. ReactJS {refs-(cont)} Thanh Tuong | ReactJS | 2016 28 Type… Click clear
  • 29. ReactJS {contents}  JSX  Virtual-DOM  Props  PropTypes  State  Refs  LifeCycle  Flux Architech  Thinking in React  Routing Thanh Tuong | ReactJS | 2016 29
  • 30. ReactJS {LifeCycle}  Each component has its own lifecycle events.  Ex:  If we wanted to make an ajax request on the initial render and fetch some data, where would we do that?  If we wanted to run some logic whenever our props changed, how would we do that?  … Thanh Tuong | ReactJS | 2016 30 LifeCycle events
  • 31. ReactJS {LifeCycle (cont)}  componentWillMount  Invoked once (both on the client and server ) before the initial render.  Good place to make connection to your db service (ex: firebase,...)  Do not call set state method here. Thanh Tuong | ReactJS | 2016 31
  • 32. ReactJS {LifeCycle (cont)}  componentDidMount  Invoked once, only on the client (not on the server).  Immediately after the initial rendering occurs.  It is good place for you to make AJAX request to fetch data for first used. Thanh Tuong | ReactJS | 2016 32
  • 33. ReactJS {LifeCycle (cont)}  componentWillReceiveProps  Invoked when a component is receiving new props.  This method is not called for the initial render.  Use this method as a way to react to a prop change before render() is called by updating the state with setState. Thanh Tuong | ReactJS | 2016 33
  • 34. ReactJS {LifeCycle (cont)}  componentWillUnmount  Invoked immediately before a component is unmounted from the DOM.  Perform any necessary cleanup in this method(Ex: invalidating timers, clear up DOM elements were created at componentDidMount) Thanh Tuong | ReactJS | 2016 34 Nothing!!! http://facebook.github.io/react/docs/component- specs.html
  • 35. ReactJS {contents}  JSX  Virtual-DOM  Props  PropTypes  State  Refs  LifeCycle  Flux Architech  Thinking in React  Routing Thanh Tuong | ReactJS | 2016 35
  • 36. ReactJS {Flux}  Flux is the application architecture.  Making data changes easy.  Remove the burden of having a component manage its own state.  The data is moved to the central called Store.  If your app doesn’t have and or care about dynamic data, Flux might not be the best choice.  Unidirectional data flow. Thanh Tuong | ReactJS | 2016 36
  • 37. ReactJS {Flux - flow} Thanh Tuong | ReactJS | 2016 37
  • 38. ReactJS {Flux - flow}  Dispatcher  Is the central hub that manages all data flow in a Flux application.  Essentially a registry of callbacks into the stores. Thanh Tuong | ReactJS | 2016 38
  • 39. ReactJS {Flux - flow} Dispatcher Store Store Store Store Thanh Tuong | ReactJS | 2016 39 When an action creator provides the dispatcher with a new action, all stores in the application receive the action via the callbacks in the registry. Broadcast
  • 40. ReactJS {Flux - flow}  Stores  Stores contain the application state and logic.  Manage the state of many objects.  Do not represent a single record of data like ORM models do.  Store registers itself with the dispatcher and provides it with a callback. Thanh Tuong | ReactJS | 2016 40
  • 41. ReactJS {Flux - flow}  Views  Typical React component.  After is mounted, it goes and get its initial state from Store and setup listener.  When it receives the event from the store, it first requests the new data it needs via the stores' public getter methods.  Then, it calls its own setState() method, causing its render() method and the render() method of all its descendants to run. Thanh Tuong | ReactJS | 2016 41
  • 42. ReactJS {Flux - flow} Thanh Tuong | ReactJS | 2016 42
  • 43. ReactJS {Flux - Implement}  Flux is just an architect. So, you can design new framework by yourself base on this architect.  Many JavaScript libraries help you implement flux like:  Flux (by Facebook: https://github.com/facebook/flux)  Reflux(by Mikael Brassman: https://github.com/reflux/refluxjs)  Redux(by Dan Abramov: https://github.com/reactjs/redux)  … Thanh Tuong | ReactJS | 2016 43
  • 44. ReactJS {Flux – source code}  https://github.com/tylermcginnis/Flux-Todolist Thanh Tuong | ReactJS | 2016 44
  • 45. ReactJS {contents}  JSX  Virtual-DOM  Props  PropTypes  State  Refs  LifeCycle  Flux Architech  Routing Thanh Tuong | ReactJS | 2016 45
  • 46. ReactJS { thinking in… } Thanh Tuong | ReactJS | 2016 46 How many components should I have? How can I break it?
  • 47. ReactJS { thinking in… } Thanh Tuong | ReactJS | 2016 47 1 4 2 2 3 3 5 5 FilterableProductTable: contains the entirety of the example 5 1 4SearchBar: receives all user input 4 ProductTable: displays and filters the data collection based on user input 2 ProductCategoryRow: displays a heading for each category 3 ProductRow: displays a row for each product
  • 48. ReactJS { thinking in… } Thanh Tuong | ReactJS | 2016 48
  • 49. ReactJS {Routing}  Make UI consistent with URL.  https://github.com/reactjs/react-router/blob/latest/docs. Thanh Tuong | ReactJS | 2016 49
  • 50. ReactJS {references}  http://tylermcginnis.com/reactjs-tutorial-a-comprehensive-guide-to- building-apps-with-react/  https://facebook.github.io/react/docs/getting-started.html  https://github.com/reactjs/react-router/tree/latest/docs  http://teropa.info/blog/2015/03/02/change-and-its-detection-in-javascript- frameworks.html  https://www.airpair.com/angularjs/posts/angular-vs-react-the-tie-breaker Thanh Tuong | ReactJS | 2016 50
  • 51. @ThanhTuong SE at KMS technology Thanh Tuong | ReactJS | 2016 51