an introduction to reactjs

43
An Introduction to React JAMES PEARCE @jamespearce

Upload: all-things-open

Post on 09-Jul-2015

2.780 views

Category:

Technology


6 download

DESCRIPTION

All Things Open 2014 - Day 2 Thursday, October 23rd, 2014 James Pearce Head of Open Source with Facebook Front Dev 1 An Introduction to ReactJS Find more by James here: https://speakerdeck.com/jamesgpearce

TRANSCRIPT

Page 1: An Introduction to ReactJS

An Introduction to ReactJAMES PEARCE@jamespearce

Page 2: An Introduction to ReactJS
Page 3: An Introduction to ReactJS

toddler

imperative programming

describe computation in terms of statements that change a

program state

teenager

declarative programming

express the logic of a computation without

describing control flow

Page 4: An Introduction to ReactJS

A JavaScript Library ForBuilding User Interfaces

Page 5: An Introduction to ReactJS

http://todomvc.com/

Page 6: An Introduction to ReactJS
Page 7: An Introduction to ReactJS
Page 8: An Introduction to ReactJS

☕(n)

Page 9: An Introduction to ReactJS

Simple !

Declarative

http://reactjs.org

Page 10: An Introduction to ReactJS

state describe

Page 11: An Introduction to ReactJS

state describe

for every change of

the whole user interface

Page 12: An Introduction to ReactJS

state describe

for every change of

the whole user interface

Page 13: An Introduction to ReactJS

state 1 state 2

Page 14: An Introduction to ReactJS

state 1 state 2

DOM mutations

Page 15: An Introduction to ReactJS

MVD #1

* Minimum Viable Demo

Page 16: An Introduction to ReactJS

<!DOCTYPE html> <html> <head> <script src="react/build/react.js"></script> <script src="react/build/JSXTransformer.js"></script> <script src="labelapp.js" type="text/jsx"></script> </head> <body/> </html>

Page 17: An Introduction to ReactJS

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

Page 18: An Introduction to ReactJS

var LabelApp = React.createClass({ render: function () { return <div>{this.props.message}</div>; } }); !React.render( <LabelApp message="Hello Raleigh!" />, document.body );

Page 19: An Introduction to ReactJS
Page 20: An Introduction to ReactJS

State & component lifecycle

Page 21: An Introduction to ReactJS

props !

passed in from parent !

<MyComp foo="bar" />

this.props read-only within !

can be defaulted & validated

state !

created within component !

getInitialState

this.state to read

this.setState() to update

Page 22: An Introduction to ReactJS

var MyComponent = React.createClass({ ! propTypes: {}, getDefaultProps: function () {}, getInitialState: function () {}, ! componentWillMount: function () {}, componentWillUnmount: function () {}, ...

render: function () { // only read props & state -> return UI }, });

Page 23: An Introduction to ReactJS

var MyComponent = React.createClass({ ! iGotClicked: function () {...}, ! render: function () { return <div onClick={this.iGotClicked}>Click</div>; }, });

Or a parent’s method passed in via props

Page 24: An Introduction to ReactJS

MVD #2

Page 25: An Introduction to ReactJS

var LabelApp = React.createClass({ getDefaultProps: function () { return {message: 'Hello World!'}; }, ! getInitialState: function () { return {message: this.props.message}; }, ! render: function() { return <div>{this.state.message}</div>; } });

Page 26: An Introduction to ReactJS

onClick: function () { this.setState({message: 'Hello Raleigh!'}); }, !render: function () { return ( <div onClick={this.onClick}> {this.state.message} </div> ); }

Page 27: An Introduction to ReactJS
Page 28: An Introduction to ReactJS

How does data flow?

Page 29: An Introduction to ReactJS

Component

Component

Component

Component

Component Component

props

props

setState

Page 30: An Introduction to ReactJS

Component

Component

Component

Component

Component Component

props

props

setState

eventcallback

Page 31: An Introduction to ReactJS

statestored as high up as it needs to be to pass down to affected children

Page 32: An Introduction to ReactJS

Table

Row Cell Cell Cell Cell

Row Cell Cell Cell Cell

Row Cell Cell Cell Cell

state ison cells

Page 33: An Introduction to ReactJS

Table

Row Cell Cell Cell Cell

Row Cell Cell Cell Cell

Row Cell Cell Cell Cell

Total

Total

Total

state ison rows

Page 34: An Introduction to ReactJS

Table

Row Cell Cell Cell Cell

Row Cell Cell Cell Cell

Row Cell Cell Cell Cell

Total

Total

Total

Row Total Total Total Total Total

state ison table

Page 35: An Introduction to ReactJS

MVD #3

Page 36: An Introduction to ReactJS

var Table = React.createClass({ getInitialState: function () { var data = []; for (var r = 0; r < this.props.rows; r++) { data[r] = []; for (var c = 0; c < this.props.columns; c++) { data[r][c] = 0; } } return {data: data}; }, render: function () {return <table>...</table>;} }); !React.render(<Table columns={4} rows={3} />, ...

Page 37: An Introduction to ReactJS

var Row = React.createClass({ render: function () { return <tr>{this.props.children}</tr>; } }); !var Total = React.createClass({ render: function () { return <th>{this.props.value}</th>; } });

Page 38: An Introduction to ReactJS

var Cell = React.createClass({ onChange: function (e) { this.props.onChange( this.props.row, this.props.column, parseInt(e.target.value) || 0 ); }, render: function () { return (<td> <input type="number" value={this.props.value} onChange={this.onChange} /> </td>); }, });

Page 39: An Introduction to ReactJS

var Table = React.createClass({ onCellChange: function(row, column, value) { this.state.data[row][column] = value; this.setState({data: this.state.data}); }, ! render: function() { // for rows & columns, append <Row>s containing // <Cell row={r} column={c} // value={this.state.data[r][c]} // onChange={this.onCellChange} /> // // also append to each row and in a final row: // <Total value={total} /> }, ...

Page 40: An Introduction to ReactJS
Page 41: An Introduction to ReactJS

Component-based UI Virtual DOM

Uni-directional data flow

Page 42: An Introduction to ReactJS

“Give it 5 minutes”

http://reactjs.org

Page 43: An Introduction to ReactJS

JAMES PEARCE@jamespearce

https://www.flickr.com/photos/matthewpaulson/6773801511 https://www.flickr.com/photos/johnath/5358512977