SlideShare una empresa de Scribd logo
1 de 12
Descargar para leer sin conexión
François Agneray
Introduction à ReactJS
21 Janvier 2016 Action nationale DevLOG « JavaScript »
Présentation
Permet de faciliter la création d'application web monopage
Les caractéristiques :

ReactJS permet de fabriquer des composants
web

Un composant ReactJS génère du code HTML à
chaque changement d'état

ReactJS ne gère que la partie interface de
l'application web (Vue)

ReactJS peut être utilisé avec une autres
bibliothèque ou un framework (AngularJS)
JSX
Les composants ReactJS sont décrits via le langage JSX
var HelloMessage = React.createClass({
render: function() {
return <div>Hello {this.props.name}</div>;
}
});
ReactDOM.render(<HelloMessage name="John" />, app);
Un premier exemple de composant simple :

Notre composant défini une méthode render()

Notre composant accepte un paramètre d'entrée name accessible via this.props

Notre composant est monté sur le nœud dont l'id est app
Babel
Les scripts JSX doivent être compilés en JavaScript
var HelloMessage = React.createClass({
render: function() {
return <div>Hello {this.props.name}</div>;
}
});
ReactDOM.render(<HelloMessage name="John" />, app);
var HelloMessage = React.createClass({
displayName: "HelloMessage",
render: function render() {
return React.createElement("div", null, "Hello ", this.props.name);
}
});
ReactDOM.render(React.createElement(HelloMessage, { name: "John" }), app);
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello React !</title>
<link rel="stylesheet" href="css/app.css" />
</head>
<body>
<div id="app"></div>
<script src="bower_components/react/react.min.js"></script>
<script src="bower_components/react/react­dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel­
core/5.8.23/browser.min.js"></script>
<script type="text/babel" src="js/helloworld.jsx"></script>
</body>
</html>
Pour fonctionner la page HTML doit :

Charger la bibliothèque ReactJS

Charger la bibliothèque Babel Browser

Spécifier le type text/babel pour les fichiers JSX
Composant avec état
Un composant ReactJS peut garder un état interne
var Timer = React.createClass({
getInitialState: function() {
return {secondsElapsed: 0};
},
tick: function() {
this.setState({secondsElapsed: this.state.secondsElapsed + 1});
},
componentDidMount: function() {
this.interval = setInterval(this.tick, 1000);
},
componentWillUnmount: function() {
clearInterval(this.interval);
},
render: function() {
return (
<div>Secondes : {this.state.secondsElapsed}</div>
);
}
});
ReactDOM.render(<Timer />, app);
Secondes : 0
Secondes : 1
Secondes : 2
...
Composant complexe
var Board = React.createClass({
render: function() {
var className = "board";
if (this.props.selected) {
className += " selected";
}
return (
<div className={className}>
{this.props.index + 1}
</div>
);
}
});
.board {
border: 5px solid #ccc;
float: left;
font: 700 24px Helvetica;
margin­right: 20px;
padding: 20px;
}
Composant complexe
var BoardSwitcher = React.createClass({
getInitialState: function() {
return {selectedIndex: 0}
},
onToggleClick: function() {
this.setState({
selectedIndex: (this.state.selectedIndex + 1) % this.props.numBoards
})
},
render: function() {
var boards = [];
for (var ii = 0; ii < this.props.numBoards; ii++) {
var isSelected = (ii === this.state.selectedIndex);
boards.push(<Board index={ii} selected={isSelected} />);
}
return (
<div>
<div className="boards">{boards}</div>
<button onClick={this.onToggleClick}>Toggle</button>
<div>selected board : {this.state.selectedIndex + 1}</div>
</div>
);
}
});
Composant complexe
React.render(
<BoardSwitcher numBoards={5} />,
document.body
);
onClick
.boards {
margin: 20px 0;
overflow: hidden;
}
.board.selected {
border­color: #3BA8AA;
}
Sudoku
http://andrey.nering.com.br/sudoku/
Netflix
Conclusion
Des exemples d'application sont disponibles sur :
https://react.rocks
La documentation et des tutoriaux sont disponibles sur :
https://facebook.github.io
http://www.overreact.io/

Más contenido relacionado

Similar a 2016_js_react.pdf

Chapitre_11-_React-Redux.pdf
Chapitre_11-_React-Redux.pdfChapitre_11-_React-Redux.pdf
Chapitre_11-_React-Redux.pdfHassanHachicha2
 
Alphorm.com Formation React : Les fondamentaux
Alphorm.com Formation React : Les fondamentauxAlphorm.com Formation React : Les fondamentaux
Alphorm.com Formation React : Les fondamentauxAlphorm
 
GWT : under the hood
GWT : under the hoodGWT : under the hood
GWT : under the hoodsvuillet
 
ENIB cours CAI Web - Séance 3 - JSP/Servlet - Cours
ENIB cours CAI Web - Séance 3 - JSP/Servlet - CoursENIB cours CAI Web - Séance 3 - JSP/Servlet - Cours
ENIB cours CAI Web - Séance 3 - JSP/Servlet - CoursHoracio Gonzalez
 
ESTEM_ReactJS_S1.pptx
ESTEM_ReactJS_S1.pptxESTEM_ReactJS_S1.pptx
ESTEM_ReactJS_S1.pptxaissamjadli
 
cours dev d'applications web moderne avec ReactJS
cours dev d'applications web moderne avec ReactJScours dev d'applications web moderne avec ReactJS
cours dev d'applications web moderne avec ReactJSaissamjadli
 
Activity
ActivityActivity
Activitydido
 
Cours yeoman backbone box2d
Cours yeoman backbone box2dCours yeoman backbone box2d
Cours yeoman backbone box2dhugomallet
 
Introduction aux RIA (Rich Internet Applications)
Introduction aux RIA (Rich Internet Applications)Introduction aux RIA (Rich Internet Applications)
Introduction aux RIA (Rich Internet Applications)Tugdual Grall
 
ENIB cours CAI Web - Séance 4 - Frameworks/Spring - Cours
ENIB cours CAI Web - Séance 4 - Frameworks/Spring - CoursENIB cours CAI Web - Séance 4 - Frameworks/Spring - Cours
ENIB cours CAI Web - Séance 4 - Frameworks/Spring - CoursHoracio Gonzalez
 
Marzouk une introduction à jdbc
Marzouk une introduction à jdbcMarzouk une introduction à jdbc
Marzouk une introduction à jdbcabderrahim marzouk
 
Tableau de bord Yammer sous SharePoint 2013
Tableau de bord Yammer sous SharePoint 2013Tableau de bord Yammer sous SharePoint 2013
Tableau de bord Yammer sous SharePoint 2013Philippe Sfeir
 
[Devoxx MA 2023] R2DBC = R2D2 + JDBC (enfin presque...)
[Devoxx MA 2023] R2DBC = R2D2 + JDBC (enfin presque...)[Devoxx MA 2023] R2DBC = R2D2 + JDBC (enfin presque...)
[Devoxx MA 2023] R2DBC = R2D2 + JDBC (enfin presque...)Bruno Bonnin
 
Café Numérique Arlon S03#02: Je code mon blog (EU code week Arlon)
Café Numérique Arlon S03#02: Je code mon blog (EU code week Arlon)Café Numérique Arlon S03#02: Je code mon blog (EU code week Arlon)
Café Numérique Arlon S03#02: Je code mon blog (EU code week Arlon)Café Numérique Arlon
 

Similar a 2016_js_react.pdf (20)

Chapitre_11-_React-Redux.pdf
Chapitre_11-_React-Redux.pdfChapitre_11-_React-Redux.pdf
Chapitre_11-_React-Redux.pdf
 
Alphorm.com Formation React : Les fondamentaux
Alphorm.com Formation React : Les fondamentauxAlphorm.com Formation React : Les fondamentaux
Alphorm.com Formation React : Les fondamentaux
 
GWT : under the hood
GWT : under the hoodGWT : under the hood
GWT : under the hood
 
ENIB cours CAI Web - Séance 3 - JSP/Servlet - Cours
ENIB cours CAI Web - Séance 3 - JSP/Servlet - CoursENIB cours CAI Web - Séance 3 - JSP/Servlet - Cours
ENIB cours CAI Web - Séance 3 - JSP/Servlet - Cours
 
ESTEM_ReactJS_S1.pptx
ESTEM_ReactJS_S1.pptxESTEM_ReactJS_S1.pptx
ESTEM_ReactJS_S1.pptx
 
cours dev d'applications web moderne avec ReactJS
cours dev d'applications web moderne avec ReactJScours dev d'applications web moderne avec ReactJS
cours dev d'applications web moderne avec ReactJS
 
Introduction à React
Introduction à ReactIntroduction à React
Introduction à React
 
Activity
ActivityActivity
Activity
 
Cours yeoman backbone box2d
Cours yeoman backbone box2dCours yeoman backbone box2d
Cours yeoman backbone box2d
 
Jboss Seam
Jboss SeamJboss Seam
Jboss Seam
 
Introduction aux RIA (Rich Internet Applications)
Introduction aux RIA (Rich Internet Applications)Introduction aux RIA (Rich Internet Applications)
Introduction aux RIA (Rich Internet Applications)
 
ENIB cours CAI Web - Séance 4 - Frameworks/Spring - Cours
ENIB cours CAI Web - Séance 4 - Frameworks/Spring - CoursENIB cours CAI Web - Séance 4 - Frameworks/Spring - Cours
ENIB cours CAI Web - Séance 4 - Frameworks/Spring - Cours
 
Marzouk une introduction à jdbc
Marzouk une introduction à jdbcMarzouk une introduction à jdbc
Marzouk une introduction à jdbc
 
1145709.ppt
1145709.ppt1145709.ppt
1145709.ppt
 
Jdbc
JdbcJdbc
Jdbc
 
Tableau de bord Yammer sous SharePoint 2013
Tableau de bord Yammer sous SharePoint 2013Tableau de bord Yammer sous SharePoint 2013
Tableau de bord Yammer sous SharePoint 2013
 
[Devoxx MA 2023] R2DBC = R2D2 + JDBC (enfin presque...)
[Devoxx MA 2023] R2DBC = R2D2 + JDBC (enfin presque...)[Devoxx MA 2023] R2DBC = R2D2 + JDBC (enfin presque...)
[Devoxx MA 2023] R2DBC = R2D2 + JDBC (enfin presque...)
 
Marzouk jsp
Marzouk jspMarzouk jsp
Marzouk jsp
 
Café Numérique Arlon S03#02: Je code mon blog (EU code week Arlon)
Café Numérique Arlon S03#02: Je code mon blog (EU code week Arlon)Café Numérique Arlon S03#02: Je code mon blog (EU code week Arlon)
Café Numérique Arlon S03#02: Je code mon blog (EU code week Arlon)
 
react-fr.pdf
react-fr.pdfreact-fr.pdf
react-fr.pdf
 

2016_js_react.pdf

  • 1. François Agneray Introduction à ReactJS 21 Janvier 2016 Action nationale DevLOG « JavaScript »
  • 2. Présentation Permet de faciliter la création d'application web monopage Les caractéristiques :  ReactJS permet de fabriquer des composants web  Un composant ReactJS génère du code HTML à chaque changement d'état  ReactJS ne gère que la partie interface de l'application web (Vue)  ReactJS peut être utilisé avec une autres bibliothèque ou un framework (AngularJS)
  • 3. JSX Les composants ReactJS sont décrits via le langage JSX var HelloMessage = React.createClass({ render: function() { return <div>Hello {this.props.name}</div>; } }); ReactDOM.render(<HelloMessage name="John" />, app); Un premier exemple de composant simple :  Notre composant défini une méthode render()  Notre composant accepte un paramètre d'entrée name accessible via this.props  Notre composant est monté sur le nœud dont l'id est app
  • 4. Babel Les scripts JSX doivent être compilés en JavaScript var HelloMessage = React.createClass({ render: function() { return <div>Hello {this.props.name}</div>; } }); ReactDOM.render(<HelloMessage name="John" />, app); var HelloMessage = React.createClass({ displayName: "HelloMessage", render: function render() { return React.createElement("div", null, "Hello ", this.props.name); } }); ReactDOM.render(React.createElement(HelloMessage, { name: "John" }), app);
  • 5. HTML <!DOCTYPE html> <html lang="en"> <head> <title>Hello React !</title> <link rel="stylesheet" href="css/app.css" /> </head> <body> <div id="app"></div> <script src="bower_components/react/react.min.js"></script> <script src="bower_components/react/react­dom.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel­ core/5.8.23/browser.min.js"></script> <script type="text/babel" src="js/helloworld.jsx"></script> </body> </html> Pour fonctionner la page HTML doit :  Charger la bibliothèque ReactJS  Charger la bibliothèque Babel Browser  Spécifier le type text/babel pour les fichiers JSX
  • 6. Composant avec état Un composant ReactJS peut garder un état interne var Timer = React.createClass({ getInitialState: function() { return {secondsElapsed: 0}; }, tick: function() { this.setState({secondsElapsed: this.state.secondsElapsed + 1}); }, componentDidMount: function() { this.interval = setInterval(this.tick, 1000); }, componentWillUnmount: function() { clearInterval(this.interval); }, render: function() { return ( <div>Secondes : {this.state.secondsElapsed}</div> ); } }); ReactDOM.render(<Timer />, app); Secondes : 0 Secondes : 1 Secondes : 2 ...
  • 7. Composant complexe var Board = React.createClass({ render: function() { var className = "board"; if (this.props.selected) { className += " selected"; } return ( <div className={className}> {this.props.index + 1} </div> ); } }); .board { border: 5px solid #ccc; float: left; font: 700 24px Helvetica; margin­right: 20px; padding: 20px; }
  • 8. Composant complexe var BoardSwitcher = React.createClass({ getInitialState: function() { return {selectedIndex: 0} }, onToggleClick: function() { this.setState({ selectedIndex: (this.state.selectedIndex + 1) % this.props.numBoards }) }, render: function() { var boards = []; for (var ii = 0; ii < this.props.numBoards; ii++) { var isSelected = (ii === this.state.selectedIndex); boards.push(<Board index={ii} selected={isSelected} />); } return ( <div> <div className="boards">{boards}</div> <button onClick={this.onToggleClick}>Toggle</button> <div>selected board : {this.state.selectedIndex + 1}</div> </div> ); } });
  • 9. Composant complexe React.render( <BoardSwitcher numBoards={5} />, document.body ); onClick .boards { margin: 20px 0; overflow: hidden; } .board.selected { border­color: #3BA8AA; }
  • 12. Conclusion Des exemples d'application sont disponibles sur : https://react.rocks La documentation et des tutoriaux sont disponibles sur : https://facebook.github.io http://www.overreact.io/