132
React and why it’s awesome Andrew Hull @aghull normative.com

React JS and why it's awesome

Embed Size (px)

DESCRIPTION

Introduction to React JS library and some of the fundamentals that set it apart from other choices

Citation preview

Page 1: React JS and why it's awesome

Reactand why it’s awesome

Andrew Hull @aghull

normative.com

Page 2: React JS and why it's awesome

React First Impressions

Page 3: React JS and why it's awesome

React First Impressions

Page 4: React JS and why it's awesome

React First Impressions

WTF?!

Page 5: React JS and why it's awesome

“Reaction” to React

Page 6: React JS and why it's awesome

“Reaction” to React

“Ugly.”

Page 7: React JS and why it's awesome

“Reaction” to React

“Ugly.”

“Separation of concerns!!”

Page 8: React JS and why it's awesome

“Reaction” to React

“Ugly.”

“Separation of concerns!!”

“React is a templating language.”

Page 9: React JS and why it's awesome

“Reaction” to React

Page 10: React JS and why it's awesome

“Reaction” to React

• Don’t sweat it

Page 11: React JS and why it's awesome

“Reaction” to React

• Don’t sweat it• JSX

Page 12: React JS and why it's awesome

“Reaction” to React

• Don’t sweat it• JSX• It’s actually just Javascript

Page 13: React JS and why it's awesome

“Reaction” to React

• Don’t sweat it• JSX• It’s actually just Javascript• It’s not a templating language

Page 14: React JS and why it's awesome

“Reaction” to React

• Don’t sweat it• JSX• It’s actually just Javascript• It’s not a templating language• If you don’t like it, you don’t hafta

Page 15: React JS and why it's awesome

React Second Impressions

Page 16: React JS and why it's awesome

React Second Impressions

• Oh! There’s 2-way data-binding, like Angular!

Page 17: React JS and why it's awesome

React Second Impressions

• Oh! There’s 2-way data-binding, like Angular!• Oh! It can work with Backbone. How I do?

Page 18: React JS and why it's awesome

React Second Impressions

• Oh! There’s 2-way data-binding, like Angular!• Oh! It can work with Backbone. How I do?• Oh! It can do animations and SVG!

Page 19: React JS and why it's awesome

React Second Impressions

• Oh! There’s 2-way data-binding, like Angular!• Oh! It can work with Backbone. How I do?• Oh! It can do animations and SVG!

Wait. Let’s back up.

Page 20: React JS and why it's awesome

Some Fundamentals

Page 21: React JS and why it's awesome

#1 Everything is a Component

Page 22: React JS and why it's awesome

React has no…

Page 23: React JS and why it's awesome

React has no…… controllers

Page 24: React JS and why it's awesome

React has no…… controllers… directives

Page 25: React JS and why it's awesome

React has no…… controllers… directives… templates

Page 26: React JS and why it's awesome

React has no…… controllers… directives… templates… global event listeners

Page 27: React JS and why it's awesome

React has no…… controllers… directives… templates… global event listeners… models

Page 28: React JS and why it's awesome

React has no…… controllers… directives… templates… global event listeners… models… no view models

Page 29: React JS and why it's awesome

React has no…… controllers… directives… templates… global event listeners… models… no view models

Just

Page 30: React JS and why it's awesome

React has no…… controllers… directives… templates… global event listeners… models… no view models

Just Components

Page 31: React JS and why it's awesome
Page 32: React JS and why it's awesome
Page 33: React JS and why it's awesome
Page 34: React JS and why it's awesome
Page 35: React JS and why it's awesome

“Separation of concerns!!”

Page 36: React JS and why it's awesome

“Separation of concerns!!”“Do you even MVC?”

Page 37: React JS and why it's awesome

What if we “separate” another way?

Page 38: React JS and why it's awesome

CartComponent

What if we “separate” another way?

Page 39: React JS and why it's awesome

CartComponent

CartListComponent

What if we “separate” another way?

Page 40: React JS and why it's awesome

CartComponent

CartListComponent

CartItemComponent

What if we “separate” another way?

Page 41: React JS and why it's awesome

CartComponent

CartListComponent

CartItemComponent

ButtonComponentButtonComponentButtonComponentButtonComponent

What if we “separate” another way?

Page 42: React JS and why it's awesome

Separation of Concerns Components

Page 43: React JS and why it's awesome

Separation of Concerns Components

•composable

Page 44: React JS and why it's awesome

Separation of Concerns Components

•composable•reusable

Page 45: React JS and why it's awesome

Separation of Concerns Components

•composable•reusable•maintainable

Page 46: React JS and why it's awesome

Separation of Concerns Components

•composable•reusable•maintainable•testable

Page 47: React JS and why it's awesome

Separation of Concerns Components

•composable•reusable•maintainable•testable

*If* Components are truly self-contained

Page 48: React JS and why it's awesome

#2 Single Source of Truth

Page 49: React JS and why it's awesome

Traditional data flows

Page 50: React JS and why it's awesome

Traditional data flowsNo framework: Any component can communicate with any other component

Page 51: React JS and why it's awesome

Traditional data flowsNo framework: Any component can communicate with any other component

Backbone: Pub-subitem.on('change:name', function() {…

Page 52: React JS and why it's awesome

Traditional data flowsNo framework: Any component can communicate with any other component

Backbone: Pub-subitem.on('change:name', function() {…

Angular: 2-way data binding and $digest loop$scope.name = …

Page 53: React JS and why it's awesome

Traditional data flowsNo framework: Any component can communicate with any other component

Backbone: Pub-subitem.on('change:name', function() {…

Angular: 2-way data binding and $digest loop$scope.name = …

React: 1-way data flow

Page 54: React JS and why it's awesome

Data handed from parent to child

check.

props.

Page 55: React JS and why it's awesome

Props accessed on this.props

Page 56: React JS and why it's awesome

Props accessed on this.props

props.

Page 57: React JS and why it's awesome

Props accessed on this.props

props.

Page 58: React JS and why it's awesome

Props accessed on this.props

props.

props.

Page 59: React JS and why it's awesome

Props accessed on this.props

props.

props.

Page 60: React JS and why it's awesome

Props are immutable

Page 61: React JS and why it's awesome

props.

Props are immutable

Page 62: React JS and why it's awesome

props.

Props are immutable

Page 63: React JS and why it's awesome

props.

Props are immutable

Don’t touch my stuff!

Page 64: React JS and why it's awesome

props.

State is mutable

Page 65: React JS and why it's awesome

props.

State is mutable

Page 66: React JS and why it's awesome

props.

State is mutable

Page 67: React JS and why it's awesome

props.

State is mutable

Whatever you say, dude.

Page 68: React JS and why it's awesome

State can become props

Page 69: React JS and why it's awesome

State can become props

props.

Page 70: React JS and why it's awesome

State can become props

props.

Page 71: React JS and why it's awesome

State can become props

props.

props.

Page 72: React JS and why it's awesome

State can become props

props.

props.

Page 73: React JS and why it's awesome

Data only flows one way

Page 74: React JS and why it's awesome

Data only flows one way

Sure, sounds good…

Page 75: React JS and why it's awesome

Data only flows one way

Sure, sounds good…(5 minutes later) Wait!

Page 76: React JS and why it's awesome

Data only flows one way

Sure, sounds good…(5 minutes later) Wait! That’s not how the real world works!

Page 77: React JS and why it's awesome

CartComponentCartListComponent

CartItemComponent

Data flows up?

<input>

Page 78: React JS and why it's awesome

CartComponent!!

<CartListComponent!list={this.state.list}!onChange={this.handleChange}/>

Data flows down with handlers

Page 79: React JS and why it's awesome

CartComponent!!

<CartListComponent!list={this.state.list}!onChange={this.handleChange}/>

CartListComponent!!

this.props.list.map(function(item) {! <CartItemComponent item={item}! onChange={this.props.onChange}/>!})

Data flows down with handlers

Page 80: React JS and why it's awesome

CartComponent!!

<CartListComponent!list={this.state.list}!onChange={this.handleChange}/>

CartListComponent!!

this.props.list.map(function(item) {! <CartItemComponent item={item}! onChange={this.props.onChange}/>!})

CartItemComponent!!changeQty: function() {! … does stuff! this.props.onChange();!}!

Data flows down with handlers

Page 81: React JS and why it's awesome

CartComponent!!

<CartListComponent!list={this.state.list}!onChange={this.handleChange}/>

CartListComponent!!

this.props.list.map(function(item) {! <CartItemComponent item={item}! onChange={this.props.onChange}/>!})

CartItemComponent!!changeQty: function() {! … does stuff! this.props.onChange();!}!

Data flows down with handlers

<input value={this.props.item.qty} onChange={this.changeQty}>

Page 82: React JS and why it's awesome

CartComponent!!

<CartListComponent!list={this.state.list}!onChange={this.handleChange}/>

CartListComponent!!

this.props.list.map(function(item) {! <CartItemComponent item={item}! onChange={this.props.onChange}/>!})

CartItemComponent!!changeQty: function() {! … does stuff! this.props.onChange();!}!

Data flows down with handlers

<input value={this.props.item.qty} onChange={this.changeQty}>

Page 83: React JS and why it's awesome

CartComponent!!

<CartListComponent!list={this.state.list}!onChange={this.handleChange}/>

CartListComponent!!

this.props.list.map(function(item) {! <CartItemComponent item={item}! onChange={this.props.onChange}/>!})

CartItemComponent!!changeQty: function() {! … does stuff! this.props.onChange();!}!

Data flows down with handlers

<input value={this.props.item.qty} onChange={this.changeQty}>

Page 84: React JS and why it's awesome

CartComponent!!

<CartListComponent!list={this.state.list}!onChange={this.handleChange}/>

CartListComponent!!

this.props.list.map(function(item) {! <CartItemComponent item={item}! onChange={this.props.onChange}/>!})

CartItemComponent!!changeQty: function() {! … does stuff! this.props.onChange();!}!

Data flows down with handlers

<input value={this.props.item.qty} onChange={this.changeQty}>

Page 85: React JS and why it's awesome

CartComponent!!

<CartListComponent!list={this.state.list}!onChange={this.handleChange}/>

CartListComponent!!

this.props.list.map(function(item) {! <CartItemComponent item={item}! onChange={this.props.onChange}/>!})

CartItemComponent!!changeQty: function() {! … does stuff! this.props.onChange();!}!

Data flows down with handlers

<input value={this.props.item.qty} onChange={this.changeQty}>

Page 86: React JS and why it's awesome

Events flow up, data flows down

Page 87: React JS and why it's awesome

Events flow up, data flows down

Does this sound familiar?

Page 88: React JS and why it's awesome

Events flow up, data flows down

Does this sound familiar?Just like the DOM.

Page 89: React JS and why it's awesome

#3 Virtual DOM

Page 90: React JS and why it's awesome

What’s worse than having state in two places at once?

Page 91: React JS and why it's awesome

What’s worse than having state in two places at once?

Having state in the DOM.

Page 92: React JS and why it's awesome

Touching the DOM is evil

Page 93: React JS and why it's awesome

Touching the DOM is evil

• It’s inconsistent

Page 94: React JS and why it's awesome

Touching the DOM is evil

• It’s inconsistent• It’s hard to test

Page 95: React JS and why it's awesome

Touching the DOM is evil

• It’s inconsistent• It’s hard to test• It’s brittle

Page 96: React JS and why it's awesome

Touching the DOM is evil

• It’s inconsistent• It’s hard to test• It’s brittle• It’s EXPENSIVE!

Page 97: React JS and why it's awesome

Back to JSX

Page 98: React JS and why it's awesome

JSX Compiled

Page 99: React JS and why it's awesome

It’s just Javascript

Page 100: React JS and why it's awesome

It’s just Javascript

Page 101: React JS and why it's awesome

Why learn yet another template language?

Page 102: React JS and why it's awesome

What’s actually happening in render() ?

Page 103: React JS and why it's awesome

What’s actually happening in render() ?

What does p() return?

Page 104: React JS and why it's awesome

What’s actually happening in render() ?

What does p() return?When do I call render()

Page 105: React JS and why it's awesome

Virtual DOM

Page 106: React JS and why it's awesome

Virtual DOM

• It’s a pure Javascript, in-memory representation of the DOM

Page 107: React JS and why it's awesome

Virtual DOM

• It’s a pure Javascript, in-memory representation of the DOM

• render() fires whenever something changes

Page 108: React JS and why it's awesome

Virtual DOM

• It’s a pure Javascript, in-memory representation of the DOM

• render() fires whenever something changes

• React modifies the real DOM to match

Page 109: React JS and why it's awesome

Virtual DOM

• It’s a pure Javascript, in-memory representation of the DOM

• render() fires whenever something changes

• React modifies the real DOM to match• It’s FAST

Page 110: React JS and why it's awesome

Virtual DOM

• It’s a pure Javascript, in-memory representation of the DOM

• render() fires whenever something changes

• React modifies the real DOM to match• It’s FAST• It’s pure

Page 111: React JS and why it's awesome

Virtual DOM

• It’s a pure Javascript, in-memory representation of the DOM

• render() fires whenever something changes

• React modifies the real DOM to match• It’s FAST• It’s pure• It just works

Page 112: React JS and why it's awesome

… Except when it doesn’t

Page 113: React JS and why it's awesome

… Except when it doesn’tHow do I access the actual DOM?

Page 114: React JS and why it's awesome

… Except when it doesn’tHow do I access the actual DOM?

How do I know when render() is done?

Page 115: React JS and why it's awesome

… Except when it doesn’tHow do I access the actual DOM?

How do I know when render() is done?

Page 116: React JS and why it's awesome

… Except when it doesn’tHow do I access the actual DOM?

How do I know when render() is done?

Lifecycle Method

Page 117: React JS and why it's awesome

… Except when it doesn’tHow do I access the actual DOM?

How do I know when render() is done?

Lifecycle Method

Actual DOM Node

Page 118: React JS and why it's awesome

Yay! Declarative Templates!

Page 119: React JS and why it's awesome

Declarative Templates suck!

Page 120: React JS and why it's awesome

Declarative Templates suck!

wut.

Page 121: React JS and why it's awesome

Declarative Templates suck!

wut.

Page 122: React JS and why it's awesome

Declarative Templates suck!

wut.

Sounds legit!

Page 123: React JS and why it's awesome

Why all this is awesome

Page 124: React JS and why it's awesome

Why all this is awesome

• One-way data flow keeps complexity under control

Page 125: React JS and why it's awesome

Why all this is awesome

• One-way data flow keeps complexity under control

• Easy to debug self-contained components

Page 126: React JS and why it's awesome

Why all this is awesome

• One-way data flow keeps complexity under control

• Easy to debug self-contained components

• Library doesn’t dictate too much

Page 127: React JS and why it's awesome

Why all this is awesome

• One-way data flow keeps complexity under control

• Easy to debug self-contained components

• Library doesn’t dictate too much• Ridiculous potential

Page 128: React JS and why it's awesome

A small demo

http://www.emergent.info/iphone-hairgate

Page 129: React JS and why it's awesome

Where to go from here?

Page 130: React JS and why it's awesome

Where to go from here?

Official React docs http://facebook.github.io/react/

Page 131: React JS and why it's awesome

Where to go from here?

Official React docs http://facebook.github.io/react/[email protected]:tastejs/todomvc.git

Page 132: React JS and why it's awesome

Thank You