Slide 1

Slide 1 text

Component KEEP SIMPLE everything can be a it

Slide 2

Slide 2 text

Pedro Nauck FRONTEND DEVELOPER @pedronauck pedronauck.com

Slide 3

Slide 3 text

USER Interfaces

Slide 4

Slide 4 text

The user interface is one of the most important parts of any program because it determines how easily you can make the program do what you want. “

Slide 5

Slide 5 text

But, what WEOUR customers want?

Slide 6

Slide 6 text

find Value

Slide 7

Slide 7 text

we need to deliver that Value

Slide 8

Slide 8 text

it's just about Design not

Slide 9

Slide 9 text

it's about design user experience quickly adapt performance scalability optimization

Slide 10

Slide 10 text

design user experience quickly adapt performance scalability otimization }Simplicity

Slide 11

Slide 11 text

complexity project size

Slide 12

Slide 12 text

Because something is simple to you, doesn't mean it's for everyone “

Slide 13

Slide 13 text

when become complex ? a project

Slide 14

Slide 14 text

questions answers

Slide 15

Slide 15 text

Is  this  a  module  or  not? “

Slide 16

Slide 16 text

Do  I  need  a  directive  here? “

Slide 17

Slide 17 text

Should  I  create  a   factory  or  a  service? “

Slide 18

Slide 18 text

Why  this  shit  doesn’t  work? “

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

complexity value

Slide 21

Slide 21 text

React how can help us with that?

Slide 22

Slide 22 text

Javascript Library for building UI

Slide 23

Slide 23 text

≄ library framework

Slide 24

Slide 24 text

library framework A tool to resolve just one specific thing A set of tools to resolve a lot of things

Slide 25

Slide 25 text

library framework

Slide 26

Slide 26 text

is using? who

Slide 27

Slide 27 text

the real proposal around

Slide 28

Slide 28 text

PROCESS simple! DEVELOPMENT is making your

Slide 29

Slide 29 text

how?

Slide 30

Slide 30 text

1 #

Slide 31

Slide 31 text

without mutation

Slide 32

Slide 32 text

without mutation We need a lightweight DOM We need control over our data We need to know what’s happening

Slide 33

Slide 33 text

WE Don't NEED

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

2 #

Slide 36

Slide 36 text

forget views models controllers modules 2-way binding observers templates {

Slide 37

Slide 37 text

FOCUS ON Components your

Slide 38

Slide 38 text

Component React is all about building reusable components. In fact, with React the only thing you do is build components. Since they're so encapsulated, components make code reuse, testing, and separation of concerns easy. http:/ /bit.ly/why-react

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Slide 41

Slide 41 text

Slide 42

Slide 42 text

Slide 43

Slide 43 text

this is the concept just

Slide 44

Slide 44 text

SHOW ME some code please!

Slide 45

Slide 45 text

NewsItem.jsx var NewsItem = React.createClass({ }); render: function() { } var React = require('react'); return (
...
... ...
    ...
);

Slide 46

Slide 46 text

NewsItem.jsx var NewsItem = React.createClass({ }); render: function() { } var React = require('react'); return (
...
... ...
    ...
);

Slide 47

Slide 47 text

NewsItem.jsx var NewsItem = React.createClass({ }); render: function() { } var React = require('react'); return (
...
... ...
    ...
); factory pattern

Slide 48

Slide 48 text

NewsItem.jsx var NewsItem = React.createClass({ }); render: function() { } var React = require('react'); return (
...
... ...
    ...
);

Slide 49

Slide 49 text

NewsItem.jsx var NewsItem = React.createClass({ }); render: function() { } var React = require('react'); return (
...
... ...
    ...
);

Slide 50

Slide 50 text

NewsItem.jsx var NewsItem = React.createClass({ }); render: function() { } var React = require('react'); return (
...
... ...
    ...
);

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

JSX A Javascript XML based extension that compile just the template part of your code

Slide 53

Slide 53 text

We strongly believe that components are the right way to separate concerns rather than "templates" and "display logic.” We think that markup and the code that generates it are intimately tied together. “

Slide 54

Slide 54 text

It makes a lot of sense that a JavaScript's template stays in the JavaScript “ @jcemer

Slide 55

Slide 55 text

What's the difference? 1 # the concerns are mixing too
<%= @post.author.name %>
<%= @post.title %>
<%= image_tag @post.image %>

Slide 56

Slide 56 text

to much better than var title = 'Some title'; var author = 'Jim Keneddy'; var newsItem = '' + '
' + author + '
' + '' + title + '' + '
' + '...' + '
' + '
    + '...' +
' + ''; 2 #

Slide 57

Slide 57 text

var NewsItem = React.createClass({ }); render: function() { } var React = require('react'); return (
...
... ...
    ...
); Will BE Compiled 3 # NewsItem.jsx

Slide 58

Slide 58 text

NewsItem.js var React = require('react'); var NewsItem = React.createClass({ render: function() { } }); return ( React.createElement('article', {className: 'news-item'}, React.createElement('div', {className: 'news-author'}, '...'), React.createElement('header', {className: 'news-title'}, '...'), React.createElement('figure', {className: 'news-cover'}, '...', React.createElement('ul', {className: 'news-stats'}, '...') ) ) );

Slide 59

Slide 59 text

But Why?

Slide 60

Slide 60 text

because is mutation EVIL

Slide 61

Slide 61 text

Virtual DOM

Slide 62

Slide 62 text

An object tree that is a representation of the DOM. Kinda a "mirror". Virtual DOM

Slide 63

Slide 63 text

Travolta DOM

Slide 64

Slide 64 text

VirtualDOM ParentComponent ChildComponent ChildComponent ChildComponent ChildComponent ChildComponent

Slide 65

Slide 65 text

EACH UPDATE VirtualDOM buid a new Virtual DOM tree diff algorithm with old compute minimal sets of mutation and put in a queue batch executes all updates

Slide 66

Slide 66 text

Old Tree Mutations New Tree VirtualDOM

Slide 67

Slide 67 text

EACH UPDATE VirtualDOM buid a new Virtual DOM tree diff algorithm with old compute minimal sets of mutation and put in a queue batch executes all updates Re-render the entire DOM

Slide 68

Slide 68 text

Advantages: Javascript is Fast and the DOM is slow

Slide 69

Slide 69 text

React Angular Time Time 0ms 0.35ms 0.7ms 1.05ms 1.4ms 1.35ms 0.31ms Render a list with 1500 rows Benchmark

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

return (
...
... ...
    ...
); NewsItem.jsx kinda messy! render: function() { }

Slide 72

Slide 72 text

WE NEED break to

Slide 73

Slide 73 text

State & Props

Slide 74

Slide 74 text

State & Props Where you manage the data of your components

Slide 75

Slide 75 text

Props State Data that can be imutable Data that can be mutable

Slide 76

Slide 76 text

Uniderectional DataFlow

Slide 77

Slide 77 text

Components can either get imutable data (via props) or management their own state (via state)

Slide 78

Slide 78 text

var React = require('react'); var NewsItem = require('./components/NewsItem'); var NewsFeed = React.createClass({ render: function() { var posts = this.props.posts.map(function(post) { return }); return
{posts}
} }); NewsFeed.jsx

Slide 79

Slide 79 text

var React = require('react'); var NewsItem = require('./components/NewsItem'); var NewsFeed = React.createClass({ render: function() { var posts = this.props.posts.map(function(post) { return }); return
{posts}
} }); NewsFeed.jsx var posts = this.props.posts.map(function(post) { return }); return
{posts}
var NewsItem = require('./components/NewsItem');

Slide 80

Slide 80 text

var React = require('react'); var NewsItem = require('./components/NewsItem'); var NewsFeed = React.createClass({ render: function() { var posts = this.props.posts.map(function(post) { return }); return
{posts}
} }); NewsFeed.jsx post={post}

Slide 81

Slide 81 text

var NewsItem = React.createClass({ }); NewsItem.jsx var React = require('react'); var NewsAuthor = require('./component/NewsAuthor'); var NewsTitle = require('./component/NewsTitle'); var NewsCover = require('./component/NewsCover'); var NewsStats = require('./component/NewsStats'); render: function() { } var post = this.props.post; return ( );

Slide 82

Slide 82 text

var NewsItem = React.createClass({ }); NewsItem.jsx var React = require('react'); var NewsAuthor = require('./component/NewsAuthor'); var NewsTitle = require('./component/NewsTitle'); var NewsCover = require('./component/NewsCover'); var NewsStats = require('./component/NewsStats'); render: function() { } var post = this.props.post; return ( );

Slide 83

Slide 83 text

var NewsItem = React.createClass({ }); NewsItem.jsx var React = require('react'); var NewsAuthor = require('./component/NewsAuthor'); var NewsTitle = require('./component/NewsTitle'); var NewsCover = require('./component/NewsCover'); var NewsStats = require('./component/NewsStats'); render: function() { } var post = this.props.post; return ( ); received data from NewsFeed

Slide 84

Slide 84 text

var NewsItem = React.createClass({ }); NewsItem.jsx var React = require('react'); var NewsAuthor = require('./component/NewsAuthor'); var NewsTitle = require('./component/NewsTitle'); var NewsCover = require('./component/NewsCover'); var NewsStats = require('./component/NewsStats'); render: function() { } var post = this.props.post; return ( ); breaking data to each component via props

Slide 85

Slide 85 text

NewsTitle.jsx var NewsTitle = React.createClass({ render: function() { var createdAt = this.props.title.createdAt; var value = this.props.title.value; return ( {createdAt}

{value}

); } });

Slide 86

Slide 86 text

NewsTitle.jsx var NewsTitle = React.createClass({ render: function() { var createdAt = this.props.title.createdAt; var value = this.props.title.value; return ( {createdAt}

{value}

); } }); {createdAt}

{value}

Slide 87

Slide 87 text

your browser will render…

Slide 88

Slide 88 text

keeping a good semantic

Slide 89

Slide 89 text

Validating props

Slide 90

Slide 90 text

NewsTitle.jsx var NewsTitle = React.createClass({ propTypes: { title: React.PropTypes.object.isRequired }, render: function() { var createdAt = this.props.title.createdAt; var value = this.props.title.value; return ( {createdAt}

{value}

); } });

Slide 91

Slide 91 text

NewsTitle.jsx var NewsTitle = React.createClass({ propTypes: { title: React.PropTypes.object.isRequired }, render: function() { var createdAt = this.props.title.createdAt; var value = this.props.title.value; return ( {createdAt}

{value}

); } }); propTypes: { title: React.PropTypes.object.isRequired }, property type if is a required property property name

Slide 92

Slide 92 text

propTypes: { optionalArray: React.PropTypes.array, optionalBool: React.PropTypes.bool, optionalFunc: React.PropTypes.func, optionalNumber: React.PropTypes.number, optionalObject: React.PropTypes.object, optionalString: React.PropTypes.string, ... }, http:/ /bit.ly/reusable-components a bunch of types and options

Slide 93

Slide 93 text

Generate a manifest of your component Throw exceptions when passed a invalid prop USEFULLfor

Slide 94

Slide 94 text

States and about

Slide 95

Slide 95 text

NewsItem.jsx var NewsItem = React.createClass({ getInitialState: function() { return { isBookmarked: false }; }, handleSelect: function(e) { this.setState({ isBookmarked: true }); e.preventDefault(); }, render: function() { var post = this.props.post; return ( ); } });

Slide 96

Slide 96 text

NewsItem.jsx var NewsItem = React.createClass({ getInitialState: function() { return { isBookmarked: false }; }, handleSelect: function(e) { this.setState({ isBookmarked: true }); e.preventDefault(); }, render: function() { var post = this.props.post; return ( ); } }); getInitialState: function() { return { isBookmarked: false }; },

Slide 97

Slide 97 text

NewsItem.jsx var NewsItem = React.createClass({ getInitialState: function() { return { isBookmarked: false }; }, handleSelect: function(e) { this.setState({ isBookmarked: true }); e.preventDefault(); }, render: function() { var post = this.props.post; return ( ); } }); handleSelect: function(e) { this.setState({ isBookmarked: true }); e.preventDefault(); },

Slide 98

Slide 98 text

NewsItem.jsx var NewsItem = React.createClass({ getInitialState: function() { return { isBookmarked: false }; }, handleSelect: function(e) { this.setState({ isBookmarked: true }); e.preventDefault(); }, render: function() { var post = this.props.post; return ( ); } }); onClick={this.handleSelect}

Slide 99

Slide 99 text

NewsItem.jsx var NewsItem = React.createClass({ getInitialState: function() { return { isBookmarked: false }; }, handleSelect: function(e) { this.setState({ isBookmarked: true }); e.preventDefault(); }, render: function() { var post = this.props.post; return ( ); } }); onClick={this.handleSelect} handleSelect: function(e) { this.setState({ isBookmarked: true }); e.preventDefault(); }, getInitialState: function() { return { isBookmarked: false }; }, managing state

Slide 100

Slide 100 text

this.setState(); VirtualDOM will re-render on each

Slide 101

Slide 101 text

var React = require('react'); var NewsFeed = require('./components/NewsFeed'); var posts = $.ajax('GET', '/some/api/url/post'); posts.success(function(response) { React.render( , document.getElementById('#myDomNode') ); }); main.jsx

Slide 102

Slide 102 text

var React = require('react'); var NewsFeed = require('./components/NewsFeed'); var posts = $.ajax('GET', '/some/api/url/post'); posts.success(function(response) { React.render( , document.getElementById('#myDomNode') ); }); main.jsx posts.success(function(response) { }); var posts = $.ajax('GET', '/some/api/url/post');

Slide 103

Slide 103 text

var React = require('react'); var NewsFeed = require('./components/NewsFeed'); var posts = $.ajax('GET', '/some/api/url/post'); posts.success(function(response) { React.render( , document.getElementById('#myDomNode') ); }); main.jsx React.render( , document.getElementById('#myDomNode') );

Slide 104

Slide 104 text

Life Cycle component

Slide 105

Slide 105 text

LifeCycle component Callbacks that become possible to interact with Virtual DOM and manipulate the real DOM

Slide 106

Slide 106 text

mounting unmounting updating componentWillMount() componentDidMount() componentWillUnmount() componentWillReceiveProps(nextProps) shouldComponentUpdate(nextProps, nextState) componentWillUpdate(nextProps, nextState) componentDidUpdate(prevProps, prevState)

Slide 107

Slide 107 text

Manage state of your component USEFULLfor Manage virtual dom render updates Integrate third-party scripts

Slide 108

Slide 108 text

var Slider = React.createClass({ render: function() { return (
  • ...
  • ...
  • ...
  • ...
); }, componentDidMount: function () { var sliderNode = this.getDOMNode(); $(sliderNode).sliderPlugin(); } }); Slider.jsx

Slide 109

Slide 109 text

var Slider = React.createClass({ render: function() { return (
  • ...
  • ...
  • ...
  • ...
); }, componentDidMount: function () { var sliderNode = this.getDOMNode(); $(sliderNode).sliderPlugin(); } }); Slider.jsx componentDidMount: function () { var sliderNode = this.getDOMNode(); $(sliderNode).sliderPlugin(); }

Slide 110

Slide 110 text

var Slider = React.createClass({ render: function() { return (
  • ...
  • ...
  • ...
  • ...
); }, componentDidMount: function () { var sliderNode = this.getDOMNode(); $(sliderNode).sliderPlugin(); } }); Slider.jsx var sliderNode = this.getDOMNode();
  • ...
  • ...
  • ...
  • ...

Slide 111

Slide 111 text

var Slider = React.createClass({ render: function() { return (
  • ...
  • ...
  • ...
  • ...
); }, componentDidMount: function () { var sliderNode = this.getDOMNode(); $(sliderNode).sliderPlugin(); } }); Slider.jsx $(sliderNode).sliderPlugin();

Slide 112

Slide 112 text

Mixins

Slide 113

Slide 113 text

Snippets of reusable code that can be injected on your component without modifying it's main behavior Mixins

Slide 114

Slide 114 text

var NewsStats = React.createClass({ render: function() { // some stuff }, checkIfAuthenticated: function() { if (MyData.isAuthenticated()) { this.setState({ isAuth: true }); } } }); NewsStats.jsx var NewsAuthor = React.createClass({ render: function() { // some stuff }, checkIfAuthenticated: function() { if (MyData.isAuthenticated()) { this.setState({ isAuth: true }); } } });

Slide 115

Slide 115 text

var NewsStats = React.createClass({ render: function() { // some stuff }, checkIfAuthenticated: function() { if (MyData.isAuthenticated()) { this.setState({ isAuth: true }); } } }); NewsStats.jsx var NewsAuthor = React.createClass({ render: function() { // some stuff }, checkIfAuthenticated: function() { if (MyData.isAuthenticated()) { this.setState({ isAuth: true }); } } }); checkIfAuthenticated: function() { if (MyData.isAuthenticated()) { this.setState({ isAuth: true }); } } checkIfAuthenticated: function() { if (MyData.isAuthenticated()) { this.setState({ isAuth: true }); } } we need to be DRY here

Slide 116

Slide 116 text

AuthMixin.jsx var AuthMixin = { getInitialState: function() { return { isAuth: false }; }, checkIfAuthenticated: function() { if (MyData.isAuthenticated()) { this.setState({ isAuth: true }); } } };

Slide 117

Slide 117 text

AuthMixin.jsx var AuthMixin = { getInitialState: function() { return { isAuth: false }; }, checkIfAuthenticated: function() { if (MyData.isAuthenticated()) { this.setState({ isAuth: true }); } } }; var AuthMixin = { };

Slide 118

Slide 118 text

AuthMixin.jsx var AuthMixin = { getInitialState: function() { return { isAuth: false }; }, checkIfAuthenticated: function() { if (MyData.isAuthenticated()) { this.setState({ isAuth: true }); } } }; checkIfAuthenticated: function() { if (MyData.isAuthenticated()) { this.setState({ isAuth: true }); } }

Slide 119

Slide 119 text

AuthMixin.jsx var AuthMixin = { getInitialState: function() { return { isAuth: false }; }, checkIfAuthenticated: function() { if (MyData.isAuthenticated()) { this.setState({ isAuth: true }); } } }; getInitialState: function() { return { isAuth: false }; },

Slide 120

Slide 120 text

NewsAuthor.jsx var AuthMixin = require('./mixins/AuthMixin'); var NewsAuthor = React.createClass({ mixins: [AuthMixin], render: function() { this.checkIfAuthenticated(); } });

Slide 121

Slide 121 text

NewsAuthor.jsx var AuthMixin = require('./mixins/AuthMixin'); var NewsAuthor = React.createClass({ mixins: [AuthMixin], render: function() { this.checkIfAuthenticated(); } }); var AuthMixin = require('./mixins/AuthMixin');

Slide 122

Slide 122 text

NewsAuthor.jsx var AuthMixin = require('./mixins/AuthMixin'); var NewsAuthor = React.createClass({ mixins: [AuthMixin], render: function() { this.checkIfAuthenticated(); } }); var NewsAuthor = React.createClass({ mixins: [AuthMixin], });

Slide 123

Slide 123 text

NewsAuthor.jsx var AuthMixin = require('./mixins/AuthMixin'); var NewsAuthor = React.createClass({ mixins: [AuthMixin], render: function() { this.checkIfAuthenticated(); } }); render: function() { this.checkIfAuthenticated(); }

Slide 124

Slide 124 text

Server Render

Slide 125

Slide 125 text

Render your component both on server and client, making your application Isomorphic and and crawlable. ServerRender

Slide 126

Slide 126 text

View layer Rounting/Controller Application Logic Client Server SPA Form Validation DOM Manipulation Animations Persistence via API

Slide 127

Slide 127 text

Isomorphic Client Server SPA Form Validation DOM Manipulation Animations View layer Persistence via API Rounting/Controller Application Logic

Slide 128

Slide 128 text

Client Server Render static markup via server Attach components behavior Render

Slide 129

Slide 129 text

var React = require('react'); var NewsFeed = require('./components/NewFeed'); var posts = api.fetchPosts(); var html = React.renderToString( ); server.renderHtml(html); server.js

Slide 130

Slide 130 text

var React = require('react'); var NewsFeed = require('./components/NewFeed'); var posts = api.fetchPosts(); var html = React.renderToString( ); server.renderHtml(html); server.js var React = require('react'); var NewsFeed = require('./components/NewFeed'); var posts = api.fetchPosts();

Slide 131

Slide 131 text

var React = require('react'); var NewsFeed = require('./components/NewFeed'); var posts = api.fetchPosts(); var html = React.renderToString( ); server.renderHtml(html); server.js var html = React.renderToString( );

Slide 132

Slide 132 text

var React = require('react'); var NewsFeed = require('./components/NewFeed'); var posts = api.fetchPosts(); var html = React.renderToString( ); server.renderHtml(html); server.js server.renderHtml(html); // just to illustrate

Slide 133

Slide 133 text

No content

Slide 134

Slide 134 text

in a nutshell React introduces a new way to build scalable applications that can take you out of your comfort zone

Slide 135

Slide 135 text

in a nutshell You need to clean your mind about a lot of old "trues"

Slide 136

Slide 136 text

in a nutshell It’s about deliver value. If something help you to make that. Why do you worry about write your HTML inside Javascript?

Slide 137

Slide 137 text

I hope you enjoyed

Slide 138

Slide 138 text

Pedro Nauck FRONTEND DEVELOPER @pedronauck pedronauck.com

Slide 139

Slide 139 text

Questions?