28
© Copyright SELA software & Education Labs Ltd. | 14-18 Baruch Hirsch St Bnei Brak, 51202 Israel | www.selagroup.com SELA DEVELOPER PRACTICE December 20 th -24 th 2015 Introduction to React Sebastian Pederiva [email protected] o.il @spederiva https://github.com/spederiva/ ReactWorkshop http://tiny.cc/react_workshop

Introduction to React

Embed Size (px)

Citation preview

Page 1: Introduction to React

© Copyright SELA software & Education Labs Ltd. | 14-18 Baruch Hirsch St Bnei Brak, 51202 Israel | www.selagroup.com

SELA DEVELOPER PRACTICEDecember 20th-24th 2015

Introduction to ReactSebastian [email protected]@spederiva

https://github.com/spederiva/ReactWorkshophttp://tiny.cc/react_workshop

Page 2: Introduction to React

AgendaReact vs AngularJsKnow ReactComponentsJSXVirtual DOMTODO demoWhy React

Page 3: Introduction to React

Don’t compare React to AngularThe V in the MVCReact is just for viewsReact don’t provide router, model and event layers

Page 4: Introduction to React

React? What?React does one thing and does it wellA library for creating user interfaces

Not a frameworkRenders your UI and responds to eventsMade by Facebook

Page 5: Introduction to React
Page 6: Introduction to React

b

Page 7: Introduction to React

<TODO> <Header></Header> <TodoList></TodoList> <Footer></Footer></TODO>

Page 8: Introduction to React

Encapsulated and interoperable custom elements that extend HTML itselfBuild components, not templatesA component should ideally only do one thingUse components to separate your concerns

var HelloWorld = React.createElement( "h1", null, "Hello World!");

React knows to build Components

Page 9: Introduction to React

JSXXML-LIKE SYNTAX EXTENSION TO ECMASCRIPT

Page 10: Introduction to React

JSXJSX lets you create JavaScript objects using HTML syntaxOptionalCombines ease-of-use of templates with power of JavaScriptPreprocessor translates to plain JavaScript:

On the fly (suitable during development)Offline using the React-CLI

http://facebook.github.io/jsx

Page 11: Introduction to React

Setup for JSX transpilershttps://facebook.github.io/react/docs/getting-started.html

Page 12: Introduction to React

<div ng-if="showFrom(message)"><div>From: {{message.from.name}}</div>

</div><div ng-if="showCreatedBy(message)">

<div>Created by: {{message.createdBy.name}}</div>

</div><div ng-if="showTo(message)">

<div>To: {{message.to.name}}</div></div>

What about separation of concerns

Page 13: Introduction to React

Reduce coupling, increase cohesionCoupling: “The degree to which each program module relies on each of the other modules.”Cohesion: “The degree to which elements of a module belong together.”Templates encourage a poor separation of concerns.

Templates separate technologies, not concerns

Page 14: Introduction to React

var HelloWorld = React.createClass({ render(){ var name = getParameterByName("name"), greet;

if (name) { greet = <h1>Hello {name}</h1>; } else { greet = <h1>Hello anonymous</h1>; }

return ( <div> {greet} </div> ) }});

What about separation of concerns

Page 15: Introduction to React

Props are select pieces of data that are passed to child components from a parent and are immutable by the child

var HelloWorld = React.createClass({ render() { return (<div>Hello {this.props.name}!!</div>); }});

ReactDOM.render(<HelloWorld name="Sebastian" />, document.getElementById("example")

);

Props

Page 16: Introduction to React

States are component data that the component sets itself via: getInitialStatethis.setState

State and UI always must be syncedhttp://facebook.github.io/react/docs/interactivity-and-dynamic-uis.html#what-components-should-have-statevar HelloWorld = React.createClass({

getInitialState(){ return{ name: "" } }, handleOnChange(e){ this.setState({ name: e.target.value }); }, render() { return (<input value={this.state.name} onChange={this.handleOnChange} />); }

});

State

Page 17: Introduction to React

StateProps State

Can get initial value from parent Component? 👍 👍

Can be changed by parent Component? 👍 ❌

Can set default values inside Component? 👍 👍

Can change inside Component? ❌ 👍

Can set initial value for child Components? 👍 👍

Page 18: Introduction to React

Component Life CycleMounting/Unmounting Update

getInitialState()getDefaultProps() componentWillMount()render()

componentWillReceiveProps() shouldComponentUpdate() componentWillUpdate() render()

componentDidMount() componentWillUnmount()

componentDidUpdate() * State before and after DOM manipulations

Page 19: Introduction to React

DOM StateData-Binding

One way bindingTwo way binding

What with the DOM?

Data Render

Page 20: Introduction to React

Re-Render All The Things

Re-render the whole app on every update

What with the DOM?

Page 21: Introduction to React

Virtual DOMAbstract version of the DOMOptimized for performance and memory footprint

Create elements for a ”Virtual DOM”Using React, you usually don't deal with DOM elements directly

Compute minimal set of changes to apply to the DOMBatch execute all updates

Page 22: Introduction to React

Re-render on Every UpdateEvery place data is displayed is guaranteed to be up-to-dateNo magical data bindingNo model dirty checkingNo more explicit DOM operationsThe re-render will occur when React decides it's ready

Page 23: Introduction to React

How it works…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 24: Introduction to React

Demo

Making a TODO list

Page 25: Introduction to React

Virtual DOM in other contextsServer renderingReact Native

https://facebook.github.io/react-native React-Canvas

https://github.com/Flipboard/react-canvas

Page 26: Introduction to React

React-CanvasReact Canvas adds the ability for React components to render to <canvas> rather than DOMMade by Flipboard

https://github.com/Flipboard/react-canvas

Sampleshttp://floppybox.baylaunch.com/https://react.rocks/tag/Canvas

Page 27: Introduction to React

Why React?Simplify codeBuild Reusable ComponentsVirtual DOMBuild Isomorphic appsEliminate querying / modifying DOMSmall API / Easy to learn & rememberSame architecture, different output

Page 28: Introduction to React

Questions