Introduction To React - Js and Redux
Introduction To React - Js and Redux
React.js(and Redux!)
1
React.js
What is a Front-End
Framework ?
2
React.js WEB Browser Beach
USER
AJAX
HTTP
HTTP HTTP
Web Dynamic
Server Web Dynamic
Dabase requests
DATA 3
React.js
Front End
Everything running on the web browser !!
Basic languages
▪ HTML
▪ CSS
▪ JAVASCRIPT
Lots of toolboxes !!
▪ Jquery
▪ AJAX
▪ Canvas
▪ WebGL
▪ …
4
React.js
5
React.js
6
What is a front end FrameWork ?
https://www.valuecoders.com/blog/wp-
content/uploads/2016/11/Angular-react-and-vue-comparision.png
What is a front end FrameWork ?
http://stefankrause.net/js-frameworks-benchmark4/webdriver-ts/table.html
What is a front end FrameWork ?
http://stateofjs.com/2016/frontend/
What is a front end FrameWork ?
https://wptavern.com/state-of-javascript-survey-results-published-react-emerges-as-
clear-winner-in-front-end-frameworks
What is a front end FrameWork ?
12
Flux and React.js Concepts
https://www.youtube.com/watch?v=Bic_sFiaNDI
13
Flux and React.js Concepts
https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html
14
Flux and React.js Concepts
16
Flux and React.js Concepts
17
Flux and React.js Concepts
18
Flux and React.js Concepts
19
Flux and React.js Concepts
Proposition: Flux
Single direction data flow: FLUX
https://facebook.github.io/flux/docs/in-depth-overview.html#content
20
Flux and React.js Concepts
Proposition: Flux
Details information and explanation here
https://facebook.github.io/flux/docs/in-depth-overview.html#content
21
Flux and React.js Concepts
UI update Issue
How updating web page efficiency after data modification ?
22
Flux and React.js Concepts
UI update Issue
How updating web page efficiency after data modifications ?
<li>
{ text: 'message 1'} <div>message 1</div>
{ text: 'message 2'} <div>message 2</div>
{ text: 'message 3'} <div>message 3</div>
<li>
23
Flux and React.js Concepts
Proposition: React.js
What is React.js?
A library for building reusable UI components
Implements one-way reactive data flow
Mostly use as the V of the MVC.
React.js Properties
Use the Javascript syntax extension (JSX)
Optimize the DOM Update through Virutal DOM
24
Flux and React.js Concepts
Web Page
Ref: http://reactkungfu.com/2015/10/the-difference-between-virtual-dom-and-dom/
25
Flux and React.js Concepts
Virtual DOM
DOM
Web Page
Initial State of DOM A state changes on A diff is computed The DOM and
and Virtual DOM the Virtual DOM between old web page are re-
Virtual Dom and render according
new Virtual DOM the computed diff
26
Flux and React.js Concepts
React.js objects
ReactElement
▪ Lowest type of virtual dom
ReactNode
▪ Hierachical Element of the virtual dom
▪ ReactElement, string, number
▪ Array of virtual nodes
ReactComponent Virtual DOM
27
Flux and React.js Concepts
React.js Component
In react.js everything (mostly) is a component
UI events for the current component
COMPONENT
Init Data
(props)
onValidate
Data
onClick (state)
onDataReceived
RENDER
VIRTUAL DOM
(for the current
component)
SPECIFICATION
28
Flux and React.js Concepts
React.js Component
import React, { Component } from 'react'; App.js
import React from 'react'; Index.js class App extends Component {
import ReactDOM from 'react-dom'; constructor(props) {
import App from './App'; super(props);
this.state = { title:this.props.title, };
ReactDOM.render(<App />, this.handleChangeTitle=this.handleChangeTitle.bind(this);
document.getElementById('root')); }
handleChangeTitle(e){
this.setState({title:e.target.value});
}
render() {
return (
<div className="App">
<h1> this is my first React Component</h1>
<label htmlFor="titleInput">Title </label>
<input type="text" id="titleInput"
onChange={this.handleChangeTitle}
value={this.state.title}
/>
<h3>{this.state.title}</h3>
</div>
); }}
export default App; 30
Flux and React.js Concepts
React.js Component
import React, { Component } from 'react'; App.js
class App extends Component {
constructor(props) {
super(props);
this.state = { title:this.props.title, };
this.handleChangeTitle=this.handleChangeTitle.bind(this); UI events for the current component
}
COMPONENT
Init Data
handleChangeTitle(e){ (props)
onValidate
this.setState({title:e.target.value}); Data
} onClick (state)
render() { onDataReceived
RENDER
VIRTUAL DOM
return ( (for the current
component)
<div className="App"> SPECIFICATION
<h1> this is my first React Component</h1>
<label htmlFor="titleInput">Title </label>
<input type="text" id="titleInput"
onChange={this.handleChangeTitle}
value={this.state.title}
/>
<h3>{this.state.title}</h3>
</div>
); }}
export default App; 31
Flux and React.js Concepts
RENDER
React.js Component
Application DOM
<html lang="en">
<head>
<meta charset="utf-8">
<title>React App</title>
</head>
<body style="">
<div id="root">
<div data-reactroot="" class="App">
<h1> this is my first React Component</h1>
<label for="titleInput">Title </label>
<input type="text" class="form-control" id="titleInput">
<h3></h3>
</div>
</div>
</body>
</html>
33
Flux and React.js Concepts
Syntax
Javascript based on the ES6 standard
Object syntactic sugar
New symbols usage (generator, arrow, functions)
Set of libraries (promises, new collections, types arrays)
34
Flux and React.js Concepts
Syntax ES6
From http://slidedeck.io/DonaldWhyte/isomorphic-react-workshop
CLASSES INHERITENCE
class Point { class ColorPoint extends Point {
constructor(x, y) { constructor(x, y, color) {
this.x = x; this.y = y; super(x, y);
} this.color = color;
toString() { return `(${this.x}, ${this.y})`; } }
} toString() { return super.toString() + ' in ' + this.color; }
}
IMPORTING OTHER MODULES
Modules are JavaScript files. EXPORTING SYMBOLS
// import foo.js in same dir as current file import foo foo.js:
from './foo'; export function foobar(num) { console.log('FOOBAR:', num); }
foo.foobar(42);
EXPORTING ENTIRE OBJECTS
// import specific variables/functions from a module person.js:
import { foobar } from './foo'; foobar(42); export default { name: 'Donald', age: 24 };
another_file.js
LET and CONST import person as './person'; console.log(person.name);
const x_const // outputs 'Donald'
Let x_var
FUNCTION
x_var = x_var +1 ()=>{return ‘hello’;}
x_const = x_const +50 // error
35
Flux and React.js Concepts
https://www.codementor.io/tamizhvendan/beginner-guide-setup-reactjs-environment-npm-babel-6-webpack-du107r9zr
36
React.js
37
Let’s go!
Installation
https://facebook.github.io/react/docs/installation.html
Nodes.js
Npm
38
Let’s go!
It is your turn !
Create App allowing to get as input a
title and print it below
Your App component must be
initialized with the title property =
‘default_title ’
39
Let’s go!
It is your turn !
Update the number of mouse over the
printed title
40
Let’s go!
Best practices
Divide your application into components
41
Let’s go!
Best practices
Divide your application into components
42
Let’s go!
Best practices
Displaying (presentational Component) Vs Processing (container Component)
VS
43
Let’s go!
Best practices
Displaying Vs Processing
44
Let’s go!
Best practices
Displaying Vs Processing
Process Data
Robot Part
Display Data
Label Properties Visual
45
Let’s go!
It is your turn !
Create the following application
Create
▪ A main component initialized with
a json file
▪ A Left side component
▪ A Robot component
▪ A Label component for the Robot
Component
It is your turn !
Main
LeftSide
Robot
Label
Visual
47
Let’s go!
It is your turn !
Same as previously but with a list of
robots
Some robot can have a visual as a video
48
Let’s go!
It is your turn !
Add a Part Component using
Description Component
Price Component
Visual Component
Add a MiddleSide Component
displaying the list of parts of the
selected robot
49
Let’s go!
It is your turn !
Main
LeftSide MiddleSide
Robot
Label
Visual
Part
Description
Price
50
Let’s go!
It is your turn !
Robots_parts.json
{"robots":[ "parts":[
{ {
"id":1, "id":"A1",
"title":"robotA", "title":"WheelA",
"visual_type":"img", "price":10
"visual_src":"https://www.robot-advance.com/EN/ori-wowwee- },
mip-white-robot-1281_1613.jpg", {
"parts":["A1","A1","A1","A1","A4"] "id":"A2",
}, "title":"WheelB",
{ "price":15
"id":2, },
"title":"robotB", {
"visual_type":"video", "id":"A3",
"visual_src":"https://www.youtube.com/embed/ePINYZK4p5Y", "title":"WheelC",
"parts":["A2","A2","A4"] "price":150
}, },
], {
"id":"A4",
"title":"Contrôleur de Servomoteurs USB SSC-32U Lynxmotion",
"price":57
}]
}
It is your turn !
Add a RightSide Component using
Panel Component displaying
selected part
52
Let’s go!
It is your turn !
Main
LeftSide MiddleSide RightSide
Robot Panel
Label
Visual
Part
Description
Price
Visual
Price
53
React.js
React.js :
Our current State
Redux as enhancement
54
Redux
55
Redux
Redux
Can be view as an
implementation of Flux
56
Redux
Redux
https://www.slideshare.net/JonasOhlsson/using-redux
57
Redux
Redux
STORE
OLD STATE
NEW STATE
COMPONENT
ACTION
ACTION REDUCER A
COMPONENT
SUBSCRIBERS
COMPONENT
REDUCER B
58
Redux
STORE
import { createStore } from 'redux';
3 Create A Store OLD STATE
REDUCER A
4 Component register
STORE
to the Store
OLD STATE
REDUCER A
SUBSCRIBERS
59
Redux
render() {
return (
<Provider store={store} >
<div className="container-fluid">
<div className="row">
<h1> Welcome to robot shop</h1>
</div>
<div className="row">
<div className="col-md-4 col-lg-4" >
<LeftSide
robots={this.state.robot_list}
/>
…
</div>
</div>
</div>
</Provider>
);
}
62
Redux
63
Redux
//connect the MiddleSide component to the store provided by the Provider Tool
// Specify a function (mapStateToProps) allowing to trig on store state modification
// when a store state is modify the mapStateToProps is launched and associate the
state.robotReducer.parts value to the local propery parts (this.props.parts)
export default connect(mapStateToProps)(MiddleSide);
64
Redux
It is your turn !
Create 2 Actions modifying the selected
Robot obj and the selected Part obj
65
Redux
It is your turn !
Modifying the previous project so as to
▪ Dispatch selected robot in the Robot
component
▪ Subscribe to store and update list of
parts in the MiddleSide component
Main
Robot Panel
Label
Part
Visual
Description
component Price
Visual
66
Réferences
References
References
68
References
Réferences
References
▪ Tutorials
https://github.com/HurricaneJames/dex/blob/master/doc/Building%20Components%20
with%20React.js%20and%20Flux.md
https://github.com/react-bootcamp/react-workshop
▪ Course tutorial
https://github.com/jacques-saraydaryan/front-end-react.js
69
Jacques Saraydaryan
Jacques.saraydaryan@cpe.fr