Top Banner
An introduction to React.js Emanuele DelBono - @emadb Community Days 2015
97
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: An introduction to React.js

An introduction to React.js

Emanuele DelBono - @emadb

Community Days 2015

Page 2: An introduction to React.js

History of front-end development…

Page 3: An introduction to React.js
Page 4: An introduction to React.js

MV* in the browser

Page 5: An introduction to React.js
Page 6: An introduction to React.js
Page 7: An introduction to React.js

Enterprise Javascript

Page 8: An introduction to React.js
Page 9: An introduction to React.js

Quickly became the de-facto standard

Page 10: An introduction to React.js

Quiz time!

Page 11: An introduction to React.js

What’s the difference between Service, Factory, Provider?

Page 12: An introduction to React.js
Page 13: An introduction to React.js

How many option do you have with ng-class?

Page 14: An introduction to React.js
Page 15: An introduction to React.js

Full & Heavy

Full framework

Ajax

Routing

Binding

Modules

Page 16: An introduction to React.js
Page 17: An introduction to React.js

#CDays15 – Milano 24, 25 e 26 Marzo 2015

Grazie a

Pla$num(Sponsor(

Page 18: An introduction to React.js

Who I am

A superhero?

Page 19: An introduction to React.js

Who I amA software developer.

Passionated about his job.

Page 20: An introduction to React.js
Page 21: An introduction to React.js

MVC? No, only the V

Page 22: An introduction to React.js

People think that React.js is awesome

Page 23: An introduction to React.js
Page 24: An introduction to React.js
Page 25: An introduction to React.js
Page 26: An introduction to React.js
Page 27: An introduction to React.js
Page 28: An introduction to React.js
Page 29: An introduction to React.js
Page 30: An introduction to React.js
Page 31: An introduction to React.js
Page 32: An introduction to React.js

Not a new idea…

Page 33: An introduction to React.js
Page 34: An introduction to React.js
Page 35: An introduction to React.js

Hello React

Page 36: An introduction to React.js

var MyComponent = React.createClass({ render: function() { return ( <h1>Hello React</h1> ); } });

React.render( <MyComponent />, document.body);

Page 37: An introduction to React.js

WHAT LANGUAGE IS THAT?

Page 38: An introduction to React.js

JSX

JSX lets you create JavaScript objects using HTML syntax

It’s optional…but once you get used to is very helpful

Page 39: An introduction to React.js

Markup with code?

Page 40: An introduction to React.js

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

React.render( React.createElement(MyComponent, null), document.body);

Page 41: An introduction to React.js

Markup with code?

“Markup and display logic both share the same concern”

Pete Hunt

Page 42: An introduction to React.js

Everything is a component

• HTML is defined inside the component

• Component logic

• State is private

• Support parameters

• Events or callback for communication

Page 43: An introduction to React.js
Page 44: An introduction to React.js
Page 45: An introduction to React.js
Page 46: An introduction to React.js

var Title = React.createClass({ render: function() { return ( <h1>Hello React</h1> ); } });

var SubTitle = React.createClass({ render: function() { return ( <h3>A library for web components</h3> ); } });

Page 47: An introduction to React.js

var Container = React.createClass({ render: function() { return ( <div>

<Title> <SubTitle>

</div> ); } });

Page 48: An introduction to React.js

Properties

• Values can be passed to the component as parameters

• Should be immutable

• Useful for passing callback (from parent to child)

Page 49: An introduction to React.js

var Container = React.createClass({ render: function() { return ( <div> <Title text="Hello react"/> <SubTitle text="A library for web components"/> </div> ); } });

Page 50: An introduction to React.js

var Title = React.createClass({ render: function() { return ( <h1>{this.props.text}</h1> ); } });

var SubTitle = React.createClass({ render: function() { return ( <h3>{this.props.text}</h3> ); } });

Page 51: An introduction to React.js

var Container = React.createClass({ render: function(){ return ( <div> <h2> Some title </h2> {this.props.children} </div>); } });

var Panel = React.createClass({ render: function(){ return ( <Container> <div>child one</div> <div>child two</div> </Container>); } });

Page 52: An introduction to React.js

State

• State is private and represent the internal situation

• State can change using provided methods

Page 53: An introduction to React.js

var Container = React.createClass({ getInitialState: function(){ return { title: this.props.title, newTitle: 'new title' } }, handleClick: function(){ this.setState({title: this.state.newTitle}); }, handleChg: function(event) { this.setState({newTitle: event.target.value}); }, render: function() { return ( <div> <h1>{this.state.title}</h1> <input type="text" value={this.state.newTitle} onChange={this.handleChg}/> <button onClick={this.handleClick}>Change title</button> </div> ); } });

Page 54: An introduction to React.js

RENDER IS CALLED EVERY TIME THE STATE

CHANGE!

Page 55: An introduction to React.js

OMG

Page 56: An introduction to React.js

Virtual DOMJavascript that access to the DOM is slow

console.dir(document.createElement('div'));

A DIV contains about 135 first level properties/function (700 on second level).

A virtual DOM div element contains only 6 properties.

Page 57: An introduction to React.js

Virtual DOM

• Our code act on a fake DOM

• React.js takes care of generating a DOM patch to update the real DOM

• Every ReactElement is a light, stateless, immutable, virtual representation of a DOM Element

Page 58: An introduction to React.js

Double Buffering

Page 59: An introduction to React.js

var ClickCounter = React.createClass({ getInitialState: function(){ console.log('getInitialState'); return {count: 0}; }, handleClick: function(){ console.log('handleClick'); this.setState({count: ++this.state.count}); }, render: function() { console.log('rendering....'); return ( <div> <button onClick={this.handleClick}>+</button> <span> {this.state.count} </span> </div> ); } });

Page 60: An introduction to React.js
Page 61: An introduction to React.js
Page 62: An introduction to React.js

WHAT ABOUT BIG

APPS?

Page 63: An introduction to React.js

Component lifecyclegetDefaultProps

getInitialState

componentWillMount

render

componentDidMount

Page 64: An introduction to React.js
Page 65: An introduction to React.js

Architecture guidelines

Page 66: An introduction to React.js
Page 67: An introduction to React.js

Flux: React Views

• The react component that we have just seen

• They receive commands from the user and send actions

Page 68: An introduction to React.js

Flux: React Views

Store

React View

State

Page 69: An introduction to React.js

Flux: Actions and ActionCreators

• ActionCreators creates actions

• Communicate with the external API

• Dispatch the actions

Page 70: An introduction to React.js

Flux: React Actions

ActionCreatorReact View

Dispatcher

ActionAPI

Page 71: An introduction to React.js

Flux: Dispachter

• Dispatch actions to the subscribers

Page 72: An introduction to React.js

Flux: Dispatcher

Store

Action

Dispatcher

Store Store

Page 73: An introduction to React.js

Flux: Store

• Manages application state

• Receives messages from the Dispatcher

• Notify views for changes

Page 74: An introduction to React.js

Flux: Store

Store

React View

Dispatcher

Change event

Request new state

Page 75: An introduction to React.js
Page 76: An introduction to React.js

How does it feel?

Page 77: An introduction to React.js
Page 78: An introduction to React.js
Page 79: An introduction to React.js
Page 80: An introduction to React.js
Page 81: An introduction to React.js
Page 82: An introduction to React.js
Page 83: An introduction to React.js
Page 84: An introduction to React.js
Page 85: An introduction to React.js
Page 86: An introduction to React.js
Page 87: An introduction to React.js
Page 88: An introduction to React.js
Page 89: An introduction to React.js
Page 90: An introduction to React.js
Page 91: An introduction to React.js
Page 92: An introduction to React.js

Browserify/Webpack

Modules done right (in the browser)

Page 93: An introduction to React.js

More here…

https://github.com/facebook/react/wiki/Complementary-Tools

https://github.com/enaqx/awesome-react

Page 94: An introduction to React.js

More topics…

PropTypes for validation

Minxin for extensibility

Server side rendering

Synthetic events

Inline style

React Native

Page 95: An introduction to React.js

<Thankyou />

@emadb

http://ema.codiceplastico.com

Page 96: An introduction to React.js
Page 97: An introduction to React.js

#CDays14 – Milano 25, 26 e 27 Febbraio 2014

Q&A

Tu#o%il%materiale%di%questa%sessione%su%h#p://www.communitydays.it/%%%Lascia%subito%il%feedback%su%questa%sessione,%potrai%essere%estra#o%per%i%nostri%premi!%%Seguici%su%

%Twi#er%@CommunityDaysIT%%Facebook%h#p://facebook.com/cdaysit%%#CDays15%