combining angular and react together

24
April 10 th 2016 Working with AngularJs 1.x and React together Sebastian Pederiva [email protected] @spederiva

Upload: sebastian-pederiva

Post on 09-Jan-2017

699 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: Combining Angular and React Together

April 10th 2016

Working with AngularJs 1.x and React together

Sebastian [email protected]

@spederiva

Page 2: Combining Angular and React Together

Don’t compare React to Angular

Page 3: Combining Angular and React Together

Don’t compare React to Angular

Page 4: Combining Angular and React Together

What is AngularJs?Framework for dynamic web appsControllers (scope)Views/TemplatesServices (service/factory/provider)FiltersBindingWatchers/$DigestEventsDirectives

Page 5: Combining Angular and React Together

What is React?A library for creating user interfacesReact does one thing and does it wellRenders your UI and responds to eventsVirtual DOMDevelop real components

Page 6: Combining Angular and React Together

Can Work Together

Page 7: Combining Angular and React Together

Build componentsEncapsulated and interoperable custom elements that extend HTML itselfA component should ideally only do one thingUse components to separate your concerns

class PageTitle extends React.Component { render() { return (<h1>Hello World!</h1>); }

}

Intro to React

Page 8: Combining Angular and React Together

JSXJSX lets you create JavaScript objects using HTML syntaxOptionalCombines ease-of-use of templates with power of JavaScripthttp://facebook.github.io/jsxNeed to be “transpiled”

https://facebook.github.io/react/docs/getting-started.htmlImprove performance with Babel

https://goo.gl/Tu9eTx

Page 9: Combining Angular and React Together

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({ function render() { return (<div>Hello {this.props.name}!!</div>); }});

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

);

Props

Page 10: Combining Angular and React Together

States are component data that the component sets itself via: getInitialStatesetState

State and UI always must be synced

var 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 11: Combining Angular and React Together

Life CycleMounting/Unmounting Update

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

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

componentDidMount() componentWillUnmount()

componentDidUpdate() * State before and after DOM manipulations

Page 12: Combining Angular and React Together

Virtual DOMAbstract JavaScript version of the DOMOptimized for performance and memory footprintRe-Render All The ThingsBatch execute all updates

Page 13: Combining Angular and React Together

Demo

TODO

Page 14: Combining Angular and React Together

Why Angular+React?WeY AngularJs YWant to use ”Components”Don’t want to think about $digestReact is very cool!Directives are powerful but not easy to write

Page 15: Combining Angular and React Together

Directives – Need to be an expert

No real ‘one way’ binding

attribute.$observe

Compile/Linkfunctions

‘ng-repeat’’track by’

Controller/Link

CompilePre-LinkPost-Link

Page 16: Combining Angular and React Together

Why Angular+React?WeY AngularJs YWant to use ”Components”Don’t want to think about $digestReact is very cool!Directives are powerful but not easy to writePerformance

Page 17: Combining Angular and React Together

Performance – AngularJs vs. React

Page 18: Combining Angular and React Together

Demo

Angular/React/Angular + React

Page 19: Combining Angular and React Together

Ng-ReactngReact is an AngularJs module that allows React Components to be used in AngularJs applicationsVery easy to useMost of the cases is enough

https://github.com/ngReact/ngReact

Page 20: Combining Angular and React Together

Demo

Ng-React

Page 21: Combining Angular and React Together

Demo

Ng-React - Bindings

Page 22: Combining Angular and React Together

Use caseRendering a really long list (ng-repeat)Very dynamic layoutPerformance problemsReally separation of concerns

Page 23: Combining Angular and React Together

Linkshttp://blog.500tech.com/using-reactjs-with-angularjshttps://www.bimeanalytics.com/engineering-blog/you-put-your-react-into-my-angularhttp://www.bennadel.com/blog/2902-rendering-reactjs-components-in-angularjs-using-angularjs-directives.htm

Page 24: Combining Angular and React Together

SummaryOverview ReactWhy AngularJs + ReactThe “glue” directive between AngularJs and ReactNg-React

Questions