intro to reactjs

95
Intro to ReactJS Jeff Winkler @winkler1 http://react.rocks

Upload: harvard-web-working-group

Post on 15-Apr-2017

3.081 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Intro to ReactJS

Intro to ReactJS

!

!

!

Jeff Winkler

@winkler1

http://react.rocks

Page 2: Intro to ReactJS
Page 3: Intro to ReactJS

What I'll be showing

• Current approaches

• React

• Demos

• Summarize

Page 4: Intro to ReactJS

Webdev Landscape in 2015

Page 5: Intro to ReactJS

JQuery! $('article.left section').click(function() {

var was_selected = $(this).hasClass('section-selected');

$('article.left section').removeClass('section-selected');

if (!was_selected) {

$(this).addClass('section-selected');

}

});

! $('article.right section').click(function() {

$(this).toggleClass('right-selected');

if ($('section.right-selected')) {

$(this).children('input.choose').toggle();

}

});

Page 6: Intro to ReactJS

Backbone

Page 7: Intro to ReactJS

Ember

Page 8: Intro to ReactJS

Angular!

• MVC

• Angular Markup: ng-model, ng-show, ng-repeat

• Dirty checking, speed limitations

• Large API.

• Scopes are inherited.

Page 9: Intro to ReactJS

Angular Scope

Page 10: Intro to ReactJS

Angular Scope

Page 11: Intro to ReactJS

Common Problems

Page 12: Intro to ReactJS
Page 13: Intro to ReactJS
Page 14: Intro to ReactJS

Shared Mutable State

Page 15: Intro to ReactJS

Core Problem

• Separation of Concerns

app/partials/button.html

app/css/button.css

app/js/directives/button.js

Page 16: Intro to ReactJS

REACT

Page 17: Intro to ReactJS

Thesis: React is

• Cohesive

!

• Easy to reason about

Page 18: Intro to ReactJS

Quick History

• Apr 2012: Instagram purchase.

• June 2013 Open-sourced

Page 19: Intro to ReactJS

Users

AirBNB, BBC, CodeAcademy, Dropbox, Facebook, Flipboard, Github, Imgur, Instagram, Khan Academy, Netflix, NYT, PayPal, Reddit, Redfin, Uber, Wired, Yahoo…

Page 20: Intro to ReactJS

Give it 5 Minutes

Page 21: Intro to ReactJS

Simple Componentvar HelloMessage = React.createClass({!

render: function() {!

return <div>Hello {this.props.name}</div>;!

}!

});!

!

React.render(<HelloMessage name="John" />, mountNode);

Page 22: Intro to ReactJS

JSXvar HelloMessage = React.createClass({!

render: function() {!

return <div>Hello {this.props.name}</div>;!

}!

});!

!

React.render(<HelloMessage name="John" />, mountNode);

Page 23: Intro to ReactJS
Page 24: Intro to ReactJS

Templating languages have a cost: they make building user interfaces harder. Doing something simple like alternating row colors in a table requires jumping through hoops in many languages. !

What we should do instead is accept that user interfaces are becoming more and more complicated and that we need a real programming language (with all of its expressive power) to build user interfaces at scale. !

(With React) instead of an oversimplified templating language, you get to use JavaScript to build abstractions and reuse code. — Pete Hunt

Page 25: Intro to ReactJS

JSXvar HelloMessage = React.createClass({!

render: function() {!

return <div>Hello {this.props.name}</div>;!

}!

});!

!

React.render(<HelloMessage name="John" />, mountNode);

Page 26: Intro to ReactJS

JSX -> createElementvar HelloMessage = React.createClass({displayName: "HelloMessage",!

render: function() {!

return React.createElement("div", null, "Hello ", this.props.name);!

}!

});!

!

React.render(React.createElement(HelloMessage, {name: "John"}), mountNode);

Page 27: Intro to ReactJS

f(data)=virtual DOMvar HelloMessage = React.createClass({!

render: function() {!

return <div>Hello {this.props.name}</div>;!

}!

});!

!

React.render(<HelloMessage name="John" />, mountNode);

Page 28: Intro to ReactJS

Using a Componentvar HelloMessage = React.createClass({!

render: function() {!

return <div>Hello {this.props.name}</div>;!

}!

});!

!

React.render(<HelloMessage name="John" />, mountNode);

Page 29: Intro to ReactJS

Passing propsvar HelloMessage = React.createClass({!

render: function() {!

return <div>Hello {this.props.name}</div>;!

}!

});!

!

React.render(<HelloMessage name="John" />, mountNode);

Page 30: Intro to ReactJS

Questions?var HelloMessage = React.createClass({!

render: function() {!

return <div>Hello {this.props.name}</div>;!

}!

});!

!

React.render(<HelloMessage name="John" />, mountNode);

Page 31: Intro to ReactJS

State

Page 32: Intro to ReactJS

Timer: has secondsElapsedvar Timer = React.createClass({!

getInitialState() {!

return {secondsElapsed: 0};!

},!

componentDidMount: function() {!

this.interval = setInterval(this.tick, 1000);!

},!

tick() {!

this.setState({secondsElapsed: this.state.secondsElapsed + 1});!

},!

render() {!

return (!

<div>Seconds Elapsed: {this.state.secondsElapsed}</div>!

);!

}!

});

Page 33: Intro to ReactJS

Initialize Statevar Timer = React.createClass({!

getInitialState() {!

return {secondsElapsed: 0};!

},!

componentDidMount: function() {!

this.interval = setInterval(this.tick, 1000);!

},!

tick() {!

this.setState({secondsElapsed: this.state.secondsElapsed + 1});!

},!

render() {!

return (!

<div>Seconds Elapsed: {this.state.secondsElapsed}</div>!

);!

}!

});

Page 34: Intro to ReactJS

setState()var Timer = React.createClass({!

getInitialState() {!

return {secondsElapsed: 0};!

},!

componentDidMount: function() {!

this.interval = setInterval(this.tick, 1000);!

},!

tick() {!

this.setState({secondsElapsed: this.state.secondsElapsed + 1});!

},!

render() {!

return (!

<div>Seconds Elapsed: {this.state.secondsElapsed}</div>!

);!

}!

});

Page 35: Intro to ReactJS

rendervar Timer = React.createClass({!

getInitialState() {!

return {secondsElapsed: 0};!

},!

componentDidMount: function() {!

this.interval = setInterval(this.tick, 1000);!

},!

tick() {!

this.setState({secondsElapsed: this.state.secondsElapsed + 1});!

},!

render() {!

return (!

<div>Seconds Elapsed: {this.state.secondsElapsed}</div>!

);!

}!

});

Page 36: Intro to ReactJS

Components -> App

Page 37: Intro to ReactJS

Compositionvar PictureOfTheDay = require('./Components/PictureOfTheDay.js');!

var DateBrowser = require('./Components/DateBrowser.js');!

var Title = require('./Components/Title.js');!

!var SpacePics = React.createClass ({!

render() {!

return (!

<div>!

<Title title={this.state.title} />!

<PictureOfTheDay picture={this.state.picture} />!

<DateBrowser date={this.state.date} onChange={this._onDateChange} />!

</div>!

);!

Page 38: Intro to ReactJS

require()var PictureOfTheDay = require('./Components/PictureOfTheDay.js');!

var DateBrowser = require('./Components/DateBrowser.js');!

var Title = require('./Components/Title.js');!

!var SpacePics = React.createClass ({!

render() {!

return (!

<div>!

<Title title={this.state.title} />!

<PictureOfTheDay picture={this.state.picture} />!

<DateBrowser date={this.state.date} onChange={this._onDateChange} />!

</div>!

);!

Page 39: Intro to ReactJS

Passing data to childrenvar PictureOfTheDay = require('./Components/PictureOfTheDay.js');!

var DateBrowser = require('./Components/DateBrowser.js');!

var Title = require('./Components/Title.js');!

!var SpacePics = React.createClass ({!

render() {!

return (!

<div>!

<Title title={this.state.title} />!

<PictureOfTheDay picture={this.state.picture} />!

<DateBrowser date={this.state.date} onChange={this._onDateChange} />!

</div>!

);!

Page 40: Intro to ReactJS

Data from parent to child

Page 41: Intro to ReactJS
Page 42: Intro to ReactJS

Need-to-knowvar PictureOfTheDay = require('./Components/PictureOfTheDay.js');!

var DateBrowser = require('./Components/DateBrowser.js');!

var Title = require('./Components/Title.js');!

!var SpacePics = React.createClass ({!

render() {!

return (!

<div>!

<Title title={this.state.title} />!

<PictureOfTheDay picture={this.state.picture} />!

<DateBrowser date={this.state.date} onChange={this._onDateChange} />!

</div>!

);!

Page 43: Intro to ReactJS

Questions?

Page 44: Intro to ReactJS

Demos

Virtual DOM!

Creating a component

ReactNative

Page 45: Intro to ReactJS

Demo #1: Virtual DOM

Page 46: Intro to ReactJS

Virtual DOM Demo: TODOMVC

Page 47: Intro to ReactJS

DOMListener

Page 48: Intro to ReactJS

Our script

• Create new: Apples, Bread

• Check off Apples

• Switch between tabs

Page 49: Intro to ReactJS

Angular

Page 50: Intro to ReactJS

Angular

Page 51: Intro to ReactJS

React

Page 52: Intro to ReactJS
Page 53: Intro to ReactJS

Demos

Virtual DOM

Creating a component!

ReactNative

Page 54: Intro to ReactJS

Demo #2: Encapsulation

• Internal state

• UI actions

• refactoring

Page 55: Intro to ReactJS
Page 56: Intro to ReactJS
Page 57: Intro to ReactJS
Page 58: Intro to ReactJS
Page 59: Intro to ReactJS
Page 60: Intro to ReactJS

Hello World• import React from 'react'; export default class WhosAsleepScore extends React.Component { render() { return ( <h1>Hello, world!</h1> ); } }

Page 61: Intro to ReactJS

Change->Hot Reload

Page 62: Intro to ReactJS

state: {count:0}

Page 63: Intro to ReactJS

code• import React from 'react'; export default class WhosAsleepScore extends React.Component { constructor(props) { super(props); this.state={count:0}; } render() { let {count}= this.state; return ( <h1>Who's Asleep score: {count}</h1> ); } }

Page 64: Intro to ReactJS

Count the sleepers!

Page 65: Intro to ReactJS

Change Request!

!

!

!

!

Page 66: Intro to ReactJS

Change display

Page 67: Intro to ReactJS

End Resultimport React from 'react'; export default class WhosAsleepScore extends React.Component { constructor(props) { super(props); this.state = {count: 0}; } addSleeper() { this.setState({count:this.state.count+1}); } render() { return ( <div> <img src='ct.png' onClick={this.addSleeper.bind(this)} /> <span style={countStyle}>{this.state.count}</span> </div> ); } }

Page 68: Intro to ReactJS

Demos

Virtual DOM

Creating a component!

ReactNative

Page 69: Intro to ReactJS

Demo #3: ReactNative

Page 70: Intro to ReactJS

react-native-spacepics

Page 71: Intro to ReactJS

Demo- Overview

Page 72: Intro to ReactJS

Components

Page 73: Intro to ReactJS

Demo-Changing Code

Page 74: Intro to ReactJS
Page 75: Intro to ReactJS

// if we have an error if (this.state.error !== null) { innerNode = <ErrorWidget title="NASA API Error" message={this.state.error} />; title = 'Error'; }

If Error

Page 76: Intro to ReactJS

ErrorWidget• var React = require('react-native'); var { StyleSheet, Text, View, } = React; class ErrorWidget extends React.Component { render() { return ( <View style={styles.container}> <Text style={styles.title}>{this.props.title}</Text> <Text style={styles.message}>{this.props.message}</Text> </View> ); } }

Page 77: Intro to ReactJS
Page 78: Intro to ReactJS

propTypesclass ErrorWidget extends React.Component {!

propTypes: {!

message: React.PropTypes.string.isRequired,!

title: React.PropTypes.string.isRequired,!

}, render() { return ( <View style={styles.container}> <Text style={styles.title}>{this.props.title}</Text> <Text style={styles.message}>{this.props.message}</Text> </View> ); } }

Page 79: Intro to ReactJS

Loading

// if we don't have a picture else if (this.state.picture === null) { innerNode = <Loading title="Getting Image..." />; title = 'Loading...'; }!

Page 80: Intro to ReactJS

Loading Widget• var React = require('react-native'); var { StyleSheet, ActivityIndicatorIOS, Text, View, } = React; class Loading extends React.Component { render() { return ( <View style={styles.container}> <ActivityIndicatorIOS animating={true} /> <Text style={styles.title}>{this.props.title}</Text> </View> ); } }

Page 81: Intro to ReactJS

else: Picture

// if we have a picture else { innerNode = <PictureOfTheDay picture={this.state.picture} />; title = this.state.picture.title; }!

Page 82: Intro to ReactJS

Components

Page 83: Intro to ReactJS
Page 84: Intro to ReactJS
Page 85: Intro to ReactJS

Demos

Virtual DOM

Creating a component

ReactNative

Page 86: Intro to ReactJS

Zooming Out

Page 87: Intro to ReactJS

Encapsulation

Page 88: Intro to ReactJS

CompositionSpacePics

PictureOfTheDay

Image

Page 89: Intro to ReactJS

Time

Page 90: Intro to ReactJS
Page 91: Intro to ReactJS

Summary

React is easy to reason about

Page 92: Intro to ReactJS
Page 93: Intro to ReactJS

Where to go from here?

• http://facebook.github.io/react/

• search “Thinking in React”

!

Page 94: Intro to ReactJS

THANK YOU

!

!

Jeff Winkler

@winkler1

http://react.rocks

Page 95: Intro to ReactJS

Reaction to JSX