intro to react - featuring modern javascript

47
Intro to React Featuring Modern JavaScript By / & / Brian Scaturro @scaturr Jason Sich @jasich

Upload: jasonsich

Post on 16-Jul-2015

166 views

Category:

Technology


8 download

TRANSCRIPT

Page 1: Intro to React - Featuring Modern JavaScript

Intro to ReactFeaturing Modern JavaScript

By / & / Brian Scaturro @scaturr Jason Sich @jasich

Page 2: Intro to React - Featuring Modern JavaScript

ReactA JavaScript library for building user interfaces

http://facebook.github.io/react/And to steal bullet point's from React's website:

Page 3: Intro to React - Featuring Modern JavaScript

JUST THE UILots of people use React as the V in MVC. Since React

makes no assumptions about the rest of yourtechnology stack, it's easy to try it out on a small

feature in an existing project.

Page 4: Intro to React - Featuring Modern JavaScript

Virtual DomReact uses a virtual DOM diff implementation forultra-high performance. It can also render on theserver using Node.js — no heavy browser DOM

required.

Page 5: Intro to React - Featuring Modern JavaScript

Data FlowReact implements one-way reactive data flow which

reduces boilerplate and is easier to reason aboutthan traditional data binding.

Page 6: Intro to React - Featuring Modern JavaScript

Components FTWIt's about separation of (application) concerns.

Page 7: Intro to React - Featuring Modern JavaScript

Thinking in components

Page 8: Intro to React - Featuring Modern JavaScript

Components as state machinesComponents have various states, and we render different views for

different states.

Page 9: Intro to React - Featuring Modern JavaScript

Components are composable render() { return <div> <MealList meals={this.state.meals} /> <ChoiceFilter /> <ChoiceList allChoices={this.state.allChoices} / > </div>}

State can live at the top and be passed down via properties.

Page 10: Intro to React - Featuring Modern JavaScript

Styling ComponentsSo like....I've heard React is all about inline styles.

Whhuuuutttt?

Page 11: Intro to React - Featuring Modern JavaScript

Well.....yes and noStyles are really just state too.

const dropState = this.getDropState('choice');let styles = {};

if (dropState.isHovering) { //yup that's state styles.backgroundColor = '#FFD34E';}

if (this.props.selection) { //yeah that's state too styles.backgroundImage = 'url(' + this.props.selection.picUrl + }

The end result is that styles benefit from reactive goodness. Statechange = style change.

Page 12: Intro to React - Featuring Modern JavaScript

And it's still HTMLAt the end of the day, the end result is an HTML document. HTML

documents can have links to stylesheets.

Page 13: Intro to React - Featuring Modern JavaScript

Using ES6 in ReactIntroducing React with idiomatic ES6.

React v0.13.0 allows for implementing components using JavaScriptclasses

JSX transformers allow for transpiling of ES6 to ES5 code

Page 14: Intro to React - Featuring Modern JavaScript

Classes and modules example import React from 'react';

class ChoiceRow extends React.Component {

}

export default ChoiceRow;

Page 15: Intro to React - Featuring Modern JavaScript

ES6 in action // ̀let̀ keywordlet choices = this.props.choices;

// arrow and map functionlet children = choices.map(c => (

<Choice item={c} />));

return <div className="row"> {children} </div>

Page 16: Intro to React - Featuring Modern JavaScript

proxies, promises, let, const, generators,etc...

Bottomline, the next evolution of JavaScript gives us a lot of cool stuff,and it would be really nice to use today.

Page 17: Intro to React - Featuring Modern JavaScript

Unfortunately...ES6 is not very well supported in most browsers.

Page 18: Intro to React - Featuring Modern JavaScript

There is hope!A lot of really smart people are working towards making ES6 usable

today.

Page 19: Intro to React - Featuring Modern JavaScript

Perceived(and maybe some actual)

Downsides

Page 20: Intro to React - Featuring Modern JavaScript

Synthetic events

Page 21: Intro to React - Featuring Modern JavaScript

Inline styles

Page 22: Intro to React - Featuring Modern JavaScript

Too much rendering?

Page 23: Intro to React - Featuring Modern JavaScript

Lots of code

Page 24: Intro to React - Featuring Modern JavaScript

Building React ApplicationsWith Flux

Immutability! Unidirectional data flow!

The dispatcher, stores and views are independentnodes with distinct inputs and outputs. The actions

are simple objects containing the new data.

https://facebook.github.io/flux/docs/overview.html#content

Page 25: Intro to React - Featuring Modern JavaScript

MVC doesn't scale

Page 26: Intro to React - Featuring Modern JavaScript

Views create new actions

Page 27: Intro to React - Featuring Modern JavaScript

Perhaps with a search form? class ChoiceFilter extends React.Component { render() { return <input type="text" onChange={this._onChange} /> }

_onChange(event) { ChoiceActions.filter(event.target.value); //create an action! }}

ChoiceActions.js

Page 28: Intro to React - Featuring Modern JavaScript

Stores respond to actionsStores respond to actions, and emit change events.

const store = new ChoiceStore();AppDispatcher.register(function (action) { switch(action.actionType) { case ChoiceConstants.CHOICE_FILTER: store.filter(action.search); store.emit(CHANGE_EVENT); //there is a change! break; default: break; }});export default store; //yup that's a single store

Page 29: Intro to React - Featuring Modern JavaScript

Views ASK for dataIn a departure from the prevalent "two-way binding" sorcery, views

listen for changes and ask for data.

Page 30: Intro to React - Featuring Modern JavaScript

An example let getState = () => { allChoices: ChoiceStore.getChoices() //asking for data}

class MealPlanner extends React.Component { constructor() { this.state = getState(); //initial state }

componentWillMount() { ChoiceStore.addChangeListener(this._onChange.bind(this)); //listen }

_onChange() { this.setState(getState()); //there was a change - ask for data

Page 31: Intro to React - Featuring Modern JavaScript

But why?!?!?!We found that two-way data bindings led to

cascading updates, where changing one object led toanother object changing, which could also trigger

more updates. As applications grew, these cascadingupdates made it very difficult to predict what wouldchange as the result of one user interaction. When

updates can only change data within a single round,the system as a whole becomes more predictable.

https://facebook.github.io/flux/docs/overview.html#content

Page 32: Intro to React - Featuring Modern JavaScript

The ToolsCurrently we have to jump through hoops to transpile ES6 to ES5, and

use handy things like JSX.

It's nice necessary to have tools to do it for us

Page 33: Intro to React - Featuring Modern JavaScript

What we want out of a workflow1. Transpile ES6 to ES52. Compile JSX to JavaScript3. Launch or reload a browser4. Compile SCSS, Jade, whatever5. Do this all when we change a file

The less we have to focus on building and running, the better

Page 34: Intro to React - Featuring Modern JavaScript

There are a lot of tools out there, here are a few we find usefulbabelbrowserifygulpwebpack

Page 35: Intro to React - Featuring Modern JavaScript

Testing With JestHey! It's worth a mention.

Page 36: Intro to React - Featuring Modern JavaScript

Oh... another Jasmine style testingframework?

The Angular team built karmaThe React team built Jest

OK

Page 37: Intro to React - Featuring Modern JavaScript

So... this one does what?Mock by defaultFake DOM via jsdom

Some new things, it's cool (but so is anything that lets you test your code)

Page 38: Intro to React - Featuring Modern JavaScript

In the land of React jest.dontMock('../ChoiceStore');jest.dontMock('../../data.json');

describe('ChoiceStore', function () { let ChoiceStore; let data;

beforeEach(function () { ChoiceStore = require('../ChoiceStore'); data = require('../../data.json'); });

describe('.getChoices()', function () { it('should return all meal options by default', function () let choices = ChoiceStore.getChoices();

Page 39: Intro to React - Featuring Modern JavaScript

A note on ES6 in testsIt requires the babel-jest package (or a similar transpile hook), and a

little tweak to your package.json file. "jest": { "scriptPreprocessor": "<rootDir>/node_modules/babel-jest", "testFileExtensions": [ "js" ], "moduleFileExtensions": [ "js" ]}

Page 40: Intro to React - Featuring Modern JavaScript

MixinsReact doesn't really go for subclassing components, but it does provide

support for the timeless JavaScript classic, the mixin.

Page 41: Intro to React - Featuring Modern JavaScript

Quick reviewA mixin is an object that typically gets merged into the prototype ofanother object - effectively adding that behavior to instances of the

target object.

Page 42: Intro to React - Featuring Modern JavaScript

Add hover functionality? let HoverMixin = { componentWillMount() { this.state = this.state || {}; this.state.hovered = false; }, componentDidMount() { this.getDomNode().addEventListener('mouseover', this._onMouseOver.bind( this.getDomNode().addEventListener('mouseout', this._onMouseOut.bind( }, componentWillUnmount() { this.getDomNode().removeEventListener('mouseover', this._onMouseOver.bind( this.getDomNode().removeEventListener('mouseout', this._onMouseOut.bind( }, _onMouseOver() {

this.setState({hovered: true});

The component* life cycle events can be duplicated. React allowsmultiple definitions for those when creating mixins.

Page 43: Intro to React - Featuring Modern JavaScript

Do the mixing import HoverMixin from 'HoverMixin';import React from 'react';

const MyComponent = React.createClass({ mixins: [HoverMixin]});

export default MyComponent;

Page 44: Intro to React - Featuring Modern JavaScript

The struggle of mixinsNote the React.createClass call. Unfortunately ES6 does not have an

official mixin mechanism, so we have to use this means of creating acomponent.

Page 45: Intro to React - Featuring Modern JavaScript

React On The Server And ClientIsomorphic JavaScript applications can run on both the server and client.React has a node module which allows it to be rendered on the server.

Page 46: Intro to React - Featuring Modern JavaScript

Future Reacthttps://github.com/reactjs/react-future

Page 47: Intro to React - Featuring Modern JavaScript

ExamplesA React app using webpack and friendshttps://github.com/jasich/meal-planner

A React app using browserify and friendshttps://github.com/brianium/plan-and-eat