Top Banner
53

React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components , , Anatomy of a Component

May 14, 2018

Download

Documents

trinhdieu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component
Page 2: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

Ben Newman (@benjamn) Paul O’Shannessy (@zpao)

React reactjs.org

Page 3: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

Components<div>, <span>

<ActionButton>, <Counter>

Page 4: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

Anatomy of a Component

Page 5: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<ActionButton text="Book flight" onAction={someFunc} />

Page 6: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<ActionButton text="Book flight" onAction={someFunc} />

var ActionButton = React.createClass({ render: function() {!!!!! }});

Page 7: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<ActionButton text="Book flight" onAction={someFunc} />

var ActionButton = React.createClass({ render: function() {!!!!! }});

Page 8: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<ActionButton text="Book flight" onAction={someFunc} />

var ActionButton = React.createClass({ render: function() { return ( <button class="ActionButton"> <span>button text</span> </button> ); }});

Page 9: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<ActionButton text="Book flight" onAction={someFunc} />

var ActionButton = React.createClass({ render: function() { return ( <button class="ActionButton"> <span>{this.props.text}</span> </button> ); }});

Page 10: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<ActionButton text="Book flight" onAction={someFunc} />

var ActionButton = React.createClass({ render: function() { return ( <button class="ActionButton" onClick={this.props.onAction}> <span>{this.props.text}</span> </button> ); }});

Page 11: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<ActionButton text="Book flight" onAction={someFunc} />

var ActionButton = React.createClass({ render: function() { return ( <button class="ActionButton" onClick={this.props.onAction}> <span>{this.props.text}</span> </button> ); }});

Page 12: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<ActionButton text="Book flight" onAction={someFunc} />

var ActionButton = React.createClass({ render: function() { return ( <button class="ActionButton" onClick={this.props.onAction}> <span>{this.props.text.toUpperCase()}</span> </button> ); }});

Page 13: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<ActionButton text="Book flight" onAction={someFunc} />

var ActionButton = React.createClass({ render: function() { return ( <button class="ActionButton" onClick={this.props.onAction}> <span>{this.props.text}</span> </button> ); }});

Page 14: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<Counter initialCount={4} />

Page 15: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<Counter initialCount={4} />

var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, addToCount: function(delta) { this.setState({count: this.state.count + delta}) }, render: function() { return ( <div> <h3>Count: {this.state.count}</h3> <ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} /> <ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} /> </div> ); }});

Page 16: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<Counter initialCount={4} />

var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, addToCount: function(delta) { this.setState({count: this.state.count + delta}) }, render: function() { return ( <div> <h3>Count: {this.state.count}</h3> <ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} /> <ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} /> </div> ); }});

Page 17: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<Counter initialCount={4} />

var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, addToCount: function(delta) { this.setState({count: this.state.count + delta}) }, render: function() { return ( <div> <h3>Count: {this.state.count}</h3> <ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} /> <ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} /> </div> ); }});

Page 18: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<Counter initialCount={4} />

var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, addToCount: function(delta) { this.setState({count: this.state.count + delta}) }, render: function() { return ( <div> <h3>Count: {this.state.count}</h3> <ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} /> <ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} /> </div> ); }});

Page 19: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<Counter initialCount={4} />

var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, addToCount: function(delta) { this.setState({count: this.state.count + delta}) }, render: function() { return ( <div> <h3>Count: {this.state.count}</h3> <ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} /> <ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} /> </div> ); }});

Page 20: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

What makes React different?

Page 21: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

1. Components, not templates

2. Re-render on update

3. Virtual DOM (and events)

Page 22: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component
Page 23: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component
Page 24: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

1. Components, not templates

Page 25: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

Separation of concerns:

!

Reduce coupling, increase cohesion.

Page 26: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

Coupling is:“The degree to which each program module relies on each of the other

modules.”

http://en.wikipedia.org/wiki/Coupling_(computer_science)

Page 27: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

Cohesion is:“The degree to which elements of a

module belong together.”

http://en.wikipedia.org/wiki/Cohesion_(computer_science)

Page 28: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

“View model” tightly couples template to

display logic.[{“price”: “7.99”, “product”: “Back

scratcher”, “tableRowColor”: “rgba(0, 0, 0, 0.5)”}]

Page 29: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

Templates separate technologies, not

concerns

Page 30: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

React components are loosely coupled and highly cohesive

Page 31: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<Counter initialCount={4} />

var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, addToCount: function(delta) { this.setState({count: this.state.count + delta}) }, render: function() { return ( <div> <h3>Count: {this.state.count}</h3> <ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} /> <ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} /> </div> ); }});

Page 32: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

2. Re-render on every change

Page 33: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

<Counter initialCount={4} />

var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, addToCount: function(delta) { this.setState({count: this.state.count + delta}) }, render: function() { return ( <div> <h3>Count: {this.state.count}</h3> <ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} /> <ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} /> </div> ); }});

- no DOM mutations - no bindings between data and DOM - in general, way less shit to think about

Page 34: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

Best analogy: Website from 1994

Page 35: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

Data changing over time is the root of all evil.

Page 36: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

Re-rendering on every change makes

things simple.Every place data is displayed is guaranteed

to be up-to-date.

Page 37: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

No magical data binding.

Re-rendering on every change makes

things simple.

Page 38: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

No model dirty checking.

Re-rendering on every change makes

things simple.

Page 39: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

No more explicit DOM operations – everything is declarative.

Re-rendering on every change makes

things simple.

Page 40: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

3. Virtual DOM

Page 41: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

Won’t rerendering be as slow as molasses?!

Page 42: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

React has a virtual DOM (and events system).

Optimized for performance and memory footprint

Page 43: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

On every update…

• React builds a new virtual DOM subtree

• …diffs it with the old one

• …computes the minimal set of DOM mutations and puts them in a queue

• …and batch executes all updates

Page 44: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

It’s fast!Because the DOM is slow!

Page 45: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

It’s fast!Computes minimal DOM operations

Page 46: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

It’s fast!Batched reads and writes for optimal DOM

performance

Page 47: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

It’s fast!Usually faster than manual DOM

operations

Page 48: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

It’s fast!Automatic top-level event delegation (with

cross-browser HTML5 events)

Page 49: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

It’s fast!Can do all this at 60fps, even in a (non-JIT)

UIWebView on the iPhone.

Page 50: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

Why Should YOU Use React?

• Can be used for parts of your application

• Plays well with other libraries and technologies (meteor, rails, node)

• Components allow you to split work easily

Page 51: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

Learn more and get involved

• http://reactjs.org

• #reactjs on Freenode IRC

• reactjs on Google Groups

• www.facebook.com/careers

Page 52: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component

More Links

• react-meteor: https://github.com/benjamn/react-meteor

• <ActionButton> demo: http://jsfiddle.net/zpao/EFhy4/

• <Clicker> demo: http://jsfiddle.net/zpao/fk5Pc/

Page 53: React - MIT 6.4706.470.scripts.mit.edu/2014/slides/React-MIT.pdf · React reactjs.org. Components <div>, <span> <ActionButton>, <Counter> Anatomy of a Component