keep
calm
and
react
!

Qui suis-je ?

React c'est quoi ?

React c'est quoi ?

Composant minimal

var html = React.DOM;

var Hello = React.createClass({
  render: function() {
    return html.p(null, 'Hello, ' + this.props.name + ' !');
  }
});  

React.renderComponent(Hello({ name: 'BordeauxJS' }), document.body);
demo

Composition

We control complexity by establishing conventional interfaces that enable us to construct systems by combining standard, well-understood pieces in a "mix and match" way. We control complexity by establishing new languages for describing a design, each of which emphasizes particular aspects of the design and deemphasizes others.
Abelson & Sussman, Structure and Interpretation of Computer Programs

Composition

Un exemple simple
var html = React.DOM;

var Hello = React.createClass({
  render: function() {
    return html.p(null, 'Hello, ' + this.props.name + ' !');
  }
});  

var HelloBordeauxJS = React.createClass({
  render: function() {
    return html.div(
      null,
      Hello({ name: 'BordeauxJS' }) // <--
    );
  }
});

React.renderComponent(HelloBordeauxJS(), document.body);      
demo

Séparation données / état

De manière générale :

Séparation données / état

var html = React.DOM;

var ClickCounter = React.createClass({
  getInitialState: function() {
    return { clickCount: 0 }; // Etat initial
  },

  handleClick: function() {
    this.setState({
      clickCount: this.state.clickCount + 1 // Mise à jour de l'état lors d'un click
    });
  },

  render: function() {
    return html.button(
      { onClick: this.handleClick }, // Callback click du bouton
      this.props.text + ' ' + this.state.clickCount // <--
    );
  }
});  

React.renderComponent(ClickCounter({ text: 'Click me !' }), document.body);
demo

En coulisses : le Virtual DOM

Lors d'un changement d'état :

Les bénéfices du Virtual DOM

Cycle de vie des composants

Compatible avec les librairies et frameworks externes

Pas de lock-in

Où sont passés mes templates ??!

Où sont passés mes templates ??!

<ul>
    <li ng-repeat="user in users">{{user.name}}</li>
</ul>
Handlebars.registerHelper('if', function(conditional, options) {
  if(conditional) {
    return options.fn(this);
  }
});

Un préprocesseur optionnel : JSX

/** @jsx React.DOM */

var Hello = React.createClass({
  render: function() {
    return <p>Hello, {this.props.name} !</p>;
  }
});  

var HelloBordeauxJS = React.createClass({
  render: function() {
    return (
      <div>
        <Hello name="BordeauxJS" />
      </div>
    );
  }
});

React.renderComponent(<HelloBordeauxJS />, document.body);      
demo

Un préprocesseur optionnel : JSX

Pourquoi React ?

Mon expérience avec React

Bilan

React en production ?

Quelques témoignages

React is awesome. It is the biggest change to how I approach web development that I can remember over the last 10+ years. It has made many things that used to be laborious and error-prone fun and robust.
Every time I finish up a React component, I'm blown away by how much simpler (and shorter) the code is compared to a traditional Backbone view — particularly when multiple subviews are involved.
I absolutely love React. It's small and does what it aims to do just great. You can probably learn React in an hour or two.
We started in Backbone, then migrated to Angular to mitigate nested view chaos. Latest rewrite drops Angular in favor of React and a bit of Backbone, mostly to reduce the learning curve for new devs. Using React for views is the simplest/most robust stack we've experienced. Easier to share/reuse code, easier to debug.
It is a serious paradigm shift [...] With React, my app got around x13 more performant.
Source : https://news.ycombinator.com/item?id=7489959

Pour conclure

Merci !

@thibautseguy github.com/thibauts