Slide 1

Slide 1 text

Rethinking the View using React Prathamesh Sonpatki @_cha1tanya

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Thank You!

Slide 4

Slide 4 text

React

Slide 5

Slide 5 text

Build UI

Slide 6

Slide 6 text

V in MVC

Slide 7

Slide 7 text

Simple

Slide 8

Slide 8 text

if encodingCompleted removeLoader(); updateHeaderText(‘Encoding completed.’) else showLoader();

Slide 9

Slide 9 text

if encodingCompleted removeLoader(); updateHeaderText(‘Encoding completed.’) else showLoader(); if encodingCompleted Encoding Completed else
Loading…

Slide 10

Slide 10 text

Unconventional ideas

Slide 11

Slide 11 text

Components

Slide 12

Slide 12 text

var Hello = React.createClass({ render: function() { return (
Hello React World!
); } }); React.render(,document.body);

Slide 13

Slide 13 text

Component is fn()

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Slide 16

Slide 16 text

Reusable

Slide 17

Slide 17 text

var TimeLine = React.createClass({ render: function() { return (
); } });

Slide 18

Slide 18 text

Composable

Slide 19

Slide 19 text

Separation of concerns

Slide 20

Slide 20 text

var Hello = React.createClass({ render: function() { return (
Hello React World!
) } });

Slide 21

Slide 21 text

var Hello = React.createClass({ render: function() { return (
Hello React World!
) } });

Slide 22

Slide 22 text

ಠ_ಠ

Slide 23

Slide 23 text

var Hello = React.createClass({ render: function() { return (
Hello React World!
) } });

Slide 24

Slide 24 text

JSX

Slide 25

Slide 25 text

JSX

Slide 26

Slide 26 text

JSX

Slide 27

Slide 27 text

JSX is so bad, it even trolls Google!

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

var Hello = React.createClass({ render: function() { return (
Hello React World!
) } });

Slide 31

Slide 31 text

JSX is like a healthy vegetable that tastes like decadent chocolate cake. You feel guilty, but it’s good. Eric Elliot

Slide 32

Slide 32 text

JSX is like Durian Prathamesh Sonpatki

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Give it 5 minutes https://signalvnoise.com/posts/3124- give-it-five-minutes

Slide 35

Slide 35 text

No templates

Slide 36

Slide 36 text

Any sufficiently complicated templating language contains an ad hoc, informally-specified, bug-ridden, slow implementation of a turing complete programming language. - Jasim A Basheer

Slide 37

Slide 37 text

var tweets = [……] tweets.map(function(tweet) { return ( ); });

Slide 38

Slide 38 text

var tweets = [……] tweets.map(function(tweet) { return ( ); });

Slide 39

Slide 39 text

React === JavaScript #=> true

Slide 40

Slide 40 text

Managing Data

Slide 41

Slide 41 text

Data changing over time is root of evil Pete Hunt

Slide 42

Slide 42 text

Props

Slide 43

Slide 43 text

Updated the website with a list of the fringe events

@reddotrubyconf

Slide 44

Slide 44 text

Props come from above

Slide 45

Slide 45 text

{this.props.author}

@{this.props.content}

Slide 46

Slide 46 text

One way data flow

Slide 47

Slide 47 text

Immutable

Slide 48

Slide 48 text

propTypes

Slide 49

Slide 49 text

var Tweet = React.createClass({ propTypes: { author: React.PropTypes.string, content: React.PropTypes.string, retweetCount: React.PropTypes.number, favoritesCount: React.PropTypes.number } });

Slide 50

Slide 50 text

State

Slide 51

Slide 51 text

State is internal

Slide 52

Slide 52 text

var Posts = React.createClass({ getInitialState: function() { return ( { posts: [] } ); }, render: function() { var posts = this.state.posts.map(function(post) { return ( ); }); } });

Slide 53

Slide 53 text

Avoid state as much as possible

Slide 54

Slide 54 text

props > state

Slide 55

Slide 55 text

Responding to state changes

Slide 56

Slide 56 text

var Posts = React.createClass({ loadPosts: function() { // fetch posts }, componentDidMount: function() { this.loadPosts(); }, render: function() { var posts = this.state.posts.map(function(post) { return ( ); }); } });

Slide 57

Slide 57 text

var Posts = React.createClass({ loadPosts: function() { // fetch posts this.setState({posts: fetchedPosts}); }, componentDidMount: function() { this.loadPosts(); }, render: function() { this.loadPosts(); var posts = this.state.posts.map(function(post) { return ( ); }); } });

Slide 58

Slide 58 text

Refresh

Slide 59

Slide 59 text

Re-Rendering the component

Slide 60

Slide 60 text

loadPosts: function() { // fetch posts this.setState({posts: fetchedPosts}); }, componentDidMount: function() { this.loadPosts(); }, render: function() { var posts = this.state.posts.map(function(post) { return ( ); }); } });

Slide 61

Slide 61 text

Re-Rendering the component tree

Slide 62

Slide 62 text

Re-Rendering

Slide 63

Slide 63 text

Isn’t it too slow?

Slide 64

Slide 64 text

Virtual DOM

Slide 65

Slide 65 text

var Hello = React.createClass({ render: function() { return (
Hello React World!
) } });

Slide 66

Slide 66 text

var Hello = React.createClass({displayName: "Hello", render: function() { return ( React.createElement("div", null, "Hello React World!" ) ) } });

Slide 67

Slide 67 text

var Hello = React.createClass({displayName: "Hello", render: function() { return ( React.createElement("div", null, "Hello React World!" ) ) } });

Slide 68

Slide 68 text

Diff

Slide 69

Slide 69 text

Minimal set of changes

Slide 70

Slide 70 text

Batch updates

Slide 71

Slide 71 text

Nothing is new!

Slide 72

Slide 72 text

React❤Rails

Slide 73

Slide 73 text

gem “react-rails”

Slide 74

Slide 74 text

rails g react:install

Slide 75

Slide 75 text

//= require react //= require react_ujs //= require components app/assets/javascripts/components app/assets/javascripts /component.js

Slide 76

Slide 76 text

JSX.to_js

Slide 77

Slide 77 text

<% @posts.each do |post| %> <%= post.author %> <%= post.content %> <% end %>

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

<% @posts.each do |post| %> <%= react_component 'Post', { post: post }, tag: 'tr' %> <% end %>

Slide 80

Slide 80 text

<% @posts.each do |post| %> <%= react_component 'Post', { post: post }, tag: 'tr' %> <% end %>

Slide 81

Slide 81 text

<% @posts.each do |post| %> <%= react_component 'Post', { post: post }, tag: 'tr' %> <% end %>

Slide 82

Slide 82 text

<% @posts.each do |post| %> <%= react_component 'Post', { post: post }, tag: 'tr' %> <% end %>

Slide 83

Slide 83 text

Slide 84

Slide 84 text

# /app/assets/javascripts/components/posts.js.jsx var Post = React.createClass({ render () { return ( {this.props.post.author} {this.props.post.content} ); } });

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

<%= react_component 'Posts', posts_url: posts_path %>

Slide 88

Slide 88 text

render: function() { setTimeout(this.loadPosts, 5000); var posts = this.state.posts.map(function(post) { return ( ); }); return (

Posts

{posts} Author Content
); }

Slide 89

Slide 89 text

render: function() { setTimeout(this.loadPosts, 5000); var posts = this.state.posts.map(function(post) { return ( ); }); return (

Posts

{posts} Author Content
); }

Slide 90

Slide 90 text

render: function() { setTimeout(this.loadPosts, 5000); var posts = this.state.posts.map(function(post) { return ( ); }); return (

Posts

{posts} Author Content
); }

Slide 91

Slide 91 text

Syncs with asset pipeline

Slide 92

Slide 92 text

config.react.variant = :development config.react.variant = :production

Slide 93

Slide 93 text

Server side rendering

Slide 94

Slide 94 text

<% @posts.each do |post| %> <%= react_component 'Post', { post: post }, { prerender: true, tag: ‘tr' } %> <% end %>

Slide 95

Slide 95 text

<% @posts.each do |post| %> <%= react_component 'Post', { post: post }, { prerender: true, tag: ‘tr' } %> <% end %>

Slide 96

Slide 96 text

Caveats • No access to document • All the dependencies must be specified in components.js • Components must be in global namespace

Slide 97

Slide 97 text

Component Generator

Slide 98

Slide 98 text

rails g react:component Tweet content:string fav_count:integer

Slide 99

Slide 99 text

var Tweet = React.createClass({ propTypes: { content: React.PropTypes.string, favCount: React.PropTypes.node }, render: function() { return (
Content: {this.props.content}
Fav Count: {this.props.fav_count}
); } });

Slide 100

Slide 100 text

React Native

Slide 101

Slide 101 text

A FRAMEWORK FOR BUILDING NATIVE APPS USING REACT

Slide 102

Slide 102 text

Learn once, write everywhere

Slide 103

Slide 103 text

Learning Curve

Slide 104

Slide 104 text

Just JavaScript

Slide 105

Slide 105 text

Just JavaScript & JSX

Slide 106

Slide 106 text

Just JavaScript

Slide 107

Slide 107 text

Easy to sneak into existing projects

Slide 108

Slide 108 text

Let me know your experience!

Slide 109

Slide 109 text

Thanks! @_cha1tanya @BigBinary React video series http://videos.bigbinary.com/categories/react http://videos.bigbinary.com/categories/tiny-reactjs-concepts