Declarative UI. Relay is the way?

We talk about JavaScript. Each month in Warsaw, Poland.

Speaker

Kamil Grabek

"Declarative UI. Relay is the way?"

2016-12-14

kamil.grabek@gmail.com

Who am I?

Front-end developer

Bluesoft, Warsaw

Tech freak, sci-fi lover, astro maniac.

"Kiedy życie zaskoczy,
pytam co ma dla mnie jeszcze"

DECLARATIVE UI
Relay is the way?

Declarative UI?

DECLARATIVE UI?

Imperative UI

IMPERATIVE UI

Example app screen

Not so long time ago...

  • MVC
  • MVP
  • MVVM
  • MV...
  • MV...WHATEVER
NOT SO LONG TIME AGO...

MVW data flow

Underscore template

            
                // EXAMPLE UNDERSCORE TEMPLATE PART I
                <div>
                  <% _.each(categories, function(category) { %>
                    <div>
                      <h2><%= category.name %></h2>
                      <h4><%= category.description %></h4>
                      <div>
                        <% _.each(category.events.edges, function(event) { %>
                          // ...
            
        

Underscore template

            
                // EXAMPLE UNDERSCORE TEMPLATE PART II
                            // ...
                            <h3><%= event.node.name %></h3>
                            <div><%= event.node.description %></div>
                          </div>
                        <% }) %>
                      </div>
                    </div>
                  <% }) %>
                </div>
            
        

MVW App

            
                // MVW EXAMPLE APP
              const DATA = {/* WHATEVER source data*/};
              const View = Backbone.View.extend({
                template: _.template(TEMPLATE),
                render: function () {
                  this.$el.html( this.template(this.model.toJSON()));
                  return this;
                }
              });
              const view = new View({ model: new Backbone.Model(DATA) });
              view.render();
              $('#root').html(view.el);
            
        

MVW example app

MVW EXAMPLE APP

Is there a better way?

IS THERE A BETTER WAY?

React!

REACT!
  • VIEW LIBRARY
  • VIRTUAL DOM
  • COMPONENTS BASED
  • HIERARCHY ORDER
  • LIFE CYCLES AND MORE ...

React data flow

Components

            
                // components.js
                class Category extends React.Component {
                  render() {
                    return <div>
                      <h2>{this.props.category.name}</h2>
                      <h4>{this.props.category.description}</h4>
                      {this.props.category.events.edges.map(({ node }) =>
                        <Event key={node.id} event={node} />
                      )}
                    </div>;
                  }
                }
                class Event extends React.Component {
                  render() {
                    return <div>
                      <h2>{this.props.event.name}</h2>
                      <h4>{this.props.event.description}</h4>
                    </div>;
                  }
                }
            
        

JSX template rendered

            
                // App.js
                import React from 'react-dom'
                import ReactDOM from 'react'
                const DATA = {/* WHATEVER source data*/};
                class App extends React.Component {
                  render() {
                    return <div>
                      {this.props.data.categories.map(category =>
                        <Category key={category.id} category={category}/>
                      )}
                    </div>;
                  }
                }
                ReactDOM.render(
                  <App data={DATA} />,
                  document.getElementById('root')
                );
            
        

React example app

REACT EXAMPLE APP

Can we do more?

CAN WE DO MORE?

Relay time!

RELAY TIME!
  • FRAMEWORK, DATA LAYER
  • HIGHER ORDER COMPONENTS
  • HIERARCHICAL CONTAINERS
  • SINGLE STORE, CACHE, UNIDIRECTIONAL DATA FLOW
  • MUTATIONS, OPTIMISTIC UPDATES AND MORE ...

Relay data flow

Relay.Route

                    
                        import Relay from 'react-relay';

                        export default class extends Relay.Route {
                          static queries = {
                            data: () => Relay.QL`query { app }`
                          };
                          static routeName = 'AppRoute';
                        }
                    
                

Relay.Container

                    
                    Category = Relay.createContainer(Category, {
                      fragments: {
                        category: () => Relay.QL`
                          fragment on Category {
                            name
                            description
                            events(first: 10) {
                              edges {
                                node {
                                  id
                                  ${Event.getFragment('event')}
                                }
                              }
                            }
                          }
                        `
                       },
                    });
                    
                

Relay.Container

                    
                        Event = Relay.createContainer(Event, {
                          fragments: {
                            event: () => Relay.QL`
                              fragment on Event {
                                name
                                description
                              }`}
                        });
                        export default Relay.createContainer(App, {
                          fragments: {
                            data: () => Relay.QL`
                              fragment on App {
                                id,
                                categories(names: ["IT", ...]) {
                                  id
                                  ${Category.getFragment('category')}
                                }
                              }`}
                        });
                    
                

Relay example app

RELAY EXAMPLE APP

MORE TO DISCOVER IN RELAY

RESOURCES

THANKS FOR
LISTENING. QUESTIONS?