KEMBAR78
ReactJS presentation | PDF
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

ReactJS presentation

  • 1.
    ReactJS The future ofweb 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} ī‚´ Creatinguser 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} ī‚´ JSXis 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} ī‚´ Usedto 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} ī‚´ Forvalidate 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} ī‚´ Tomanage 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)} ī‚´ Whenyou should use state? ī‚´ Respond to user input. ī‚´ Server request. ī‚´ or the passage of time. Thanh Tuong | ReactJS | 2016 24
  • 25.
    ReactJS { propsvs 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} ī‚´ Howwe 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} ī‚´ Eachcomponent 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} ī‚´ Fluxis 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 { thinkinginâ€Ļ } Thanh Tuong | ReactJS | 2016 46 How many components should I have? How can I break it?
  • 47.
    ReactJS { thinkinginâ€Ļ } 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 { thinkinginâ€Ļ } Thanh Tuong | ReactJS | 2016 48
  • 49.
    ReactJS {Routing} ī‚´ MakeUI 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 KMStechnology Thanh Tuong | ReactJS | 2016 51