React & Flux

By Robert Katzki

React & Flux

by Robert Katzki

@RobertKatzki

Ubilabs

React

facebook.github.io/react/

React is the V in MVC

Use whatever framework you like

On data changes,
the whole DOM
is rerendered.

90s style refresh!

But … that's slow!

Introducing:
Virtual DOM

Minimal DOM operations

Side effect:
Server side rendering

Everything is a component

          /** @jsx React.DOM */
          var Hello = React.createClass({
            render: function() {
              return <div>Hello {this.props.name}</div>;
            }
          });
            
          React.renderComponent(<Hello name="HH.js" />, document.body);
        
          /** @jsx React.DOM */
          var Hello = React.createClass({
            render: function() {
              return <div>Hello {this.props.name}</div>;
            }
          });
            
          React.renderComponent(<Hello name="HH.js" />, document.body);
        
          /** @jsx React.DOM */
          var Hello = React.createClass({
            render: function() {
              return <div>Hello {this.props.name}</div>;
            }
          });
            
          React.renderComponent(<Hello name="HH.js" />, document.body);
        
          /** @jsx React.DOM */
          var Hello = React.createClass({
            render: function() {
              return <div>Hello {this.props.name}</div>;
            }
          });
            
          React.renderComponent(<Hello name="HH.js" />, document.body);
        
          /** @jsx React.DOM */
          var Hello = React.createClass({
            render: function() {
              return <div>Hello {this.props.name}</div>;
            }
          });
            
          React.renderComponent(<Hello name="HH.js" />, document.body);
        

Wait, whats' that?

Introducing:
JSX

Write JavaScript with HTML syntax.

Introducing:
JSX

It is not required.

          /** @jsx React.DOM */
          var Hello = React.createClass({
            render: function() {
              return <div>Hello {this.props.name}</div>;
            }
          });
            
          React.renderComponent(<Hello name="HH.js" />, document.body);
        
          /** @jsx React.DOM */
          var Hello = React.createClass({displayName: 'Hello',
            render: function() {
              return React.DOM.div(null, "Hello ", this.props.name);
            }
          });
            
          React.renderComponent(Hello({name: "HH.js"}), document.body);
        

Components
are just functions

The output should be the same at any time

What about separation of concerns?

Separating concerns.

React only fulfills the View.

Separating concerns.

Markup and layout depend on each other.

Separating concerns.

Not separating technologies.

One way data flow

Components
have a lifecycle

getDefaultProps()

Components
have props and state

Changeable state kept in a component

state can be passed to child components and is available as prop

Safe to use?

It's been battle tested at:

react-components.com

React Native

facebook.github.io/react-native/

Learn once, write anywhere

Build native applications with JavaScript

Use the standard platform components

          var React = require('react-native');
          var { TabBarIOS, NavigatorIOS } = React;
          var App = React.createClass({
            render: function() {return (
              <TabBarIOS>
                <TabBarIOS.Item title="React Native" selected={true}>
                  <NavigatorIOS initialRoute={{ title: 'React Native' }} />
                </TabBarIOS.Item>
              </TabBarIOS>
            );}
          });
        
          var React = require('react-native');
          var { TabBarIOS, NavigatorIOS } = React;
          var App = React.createClass({
            render: function() {return (
              <TabBarIOS>
                <TabBarIOS.Item title="React Native" selected={true}>
                  <NavigatorIOS initialRoute={{ title: 'React Native' }} />
                </TabBarIOS.Item>
              </TabBarIOS>
            );}
          });
        

Fast development with hot module reloading

https://youtu.be/7rDsRXj9-cU?t=22m14s

Currently only for iOS

Android?

www.reactnativeandroid.com

Flux

facebook.github.io/flux/

It's not a framework.
It's a pattern.

Complements React

Unidirectional data flow

Stores contain the application state and logic

Dispatcher is the central hub that manages all data flow

Action creators are the main controllers of the app

Views listen to stores and show the data

Actions start a data cycle

Asynchronous functions belong into Actions

Implementations

Flux Comparison by Example

Thanks!

Questions?

One more thing…

2015.jsunconf.eu

@jsunconf

Fork me on Github