Slide 1

Slide 1 text

E S 6 + R E A C T Up B R I A N H O LT

Slide 2

Slide 2 text

@ H O LT B T – B R I @ N H O . LT P R E V I O U S LY C U R R E N T LY Frozen

Slide 3

Slide 3 text

B T H O LT. G I T H U B . I O / E S 6 - R E A C T- P R E S AKA Jessica Jones

Slide 4

Slide 4 text

U N D E R S TA N D I N G D E M O G R A P H I C • WTF? I could teach this class. • WTF is React? • WTF is ES6? • WTF is both ES6 and React? • WTF is JavaScript? • WTF is a computer? • WTF is life? House of Cards }This class best addresses these people } And these

Slide 5

Slide 5 text

E S 6 F E AT U R E S B R I A N H O LT House of Cards

Slide 6

Slide 6 text

E S 6 C O N V E N I E N C E S • Enhanced object literal syntax • Default params • Template strings • const and let • codepen.io/btholt/pen/mJQWjQ • Exercises: codepen.io/btholt/pen/QjxRPN Better Call Saul

Slide 7

Slide 7 text

E S 6 C O N V E N I E N C E S • Destructuring • Spread / rest operator • For … of loops • codepen.io/btholt/pen/eNQvov • Exercises: codepen.io/btholt/pen/rOrBxe Hemlock Grove

Slide 8

Slide 8 text

A R R O W F U N C T I O N S • From CoffeeScript • Shorthand for function • Autobinds to lexical scope • codepen.io/btholt/pen/RPqKxp • Exercises: codepen.io/btholt/pen/qOyWrP Daredevil

Slide 9

Slide 9 text

C L A S S E S • Syntactical sugar! • Looks like classical inheritance, but not. • Useful for the new version of ES6 • codepen.io/btholt/pen/QbJveO BoJack Horseman

Slide 10

Slide 10 text

P R O M I S E S • Finally built straight into JS • Easier way of dealing with callbacks • Work well with AJAX • codepen.io/btholt/pen/gpQRRW • Exercises: codepen.io/btholt/pen/PPBGdQ Wet Hot American Summer: First Day of Camp

Slide 11

Slide 11 text

G E N E R AT O R S • New construct to ES6 that enablew new patterns of asynchronous programming that looks synchronous. • Can greatly reduce the complexity of some problems. • Can be confusing due to their versatility in how they handle input and output. • github.com/btholt/generator-examples • speakerdeck.com/btholt/unlimited-power-es6-generators Bloodline

Slide 12

Slide 12 text

O T H E R E S 6 F E AT U R E S • Better Unicode support • Modules • Maps / sets • New object and Math APIs Unbreakable Kimmy Schmidt

Slide 13

Slide 13 text

O T H E R E S 6 F E AT U R E S • Proxies • Symbols • Weak maps / weak sets • Tail call recursion Arrested Development

Slide 14

Slide 14 text

@ H O LT B T Orange is the New Black

Slide 15

Slide 15 text

W H AT I S R E A C T ? • React is open source library that came out of Facebook. • It is a view layer that allows you to create components that are in turn composed of other components. • Its greatest contribution to the JS landscape is the solidifying and popularizing the concept of one-way data flow as applied JS user interface construction. • It does use a virtual DOM to achieve great performance in the browser. While a really great feature and well implemented, this only makes using React feasible, not desirable. Indiana Jones and the Last Crusade

Slide 16

Slide 16 text

“ S TAT E I S T H E E N E M Y O F A M A I N TA I N A B L E A P P. ” — B A T M A N The Dark Knight

Slide 17

Slide 17 text

S TAT E V S P R O P S • React has two concepts of data access in a component: state and props. • Props are “things” passed down from the parent to the child. Try to use props as much as possible and keep as little state as possible. Props are immutable. • State are kept in a component and can only be modified in that component. State is mutable. Using state should avoided where possible, opting to use as little state as possible. Kill Bill Vol. 1

Slide 18

Slide 18 text

O N E WAY D ATA F L O W • React views are made of a component, which is in turn made of components, which is in turn made of up of more components. Turtles all the way down. • Data only flows from the parents to the children. • Children thus only have the logic to display the data, not modify it. • Children also handle events and then inform parents via callback or events. Parents then modify their own state. • Thus when you unexpected behavior it can only live in a select few places. This comes at the cost of being a bit verbose. Skyfall

Slide 19

Slide 19 text

S O W H AT D O I D O W I T H D ATA I F R E A C T D O E S N ’ T C A R E A B O U T I T ? The Lego Movie wtf state

Slide 20

Slide 20 text

U P T O Y O U • Use props and state; store much of the state in the root component. I typically do it this way unless I have reasons not to. • Need cross, unrelated component communication? (think Facebook’s notification bar and how the number goes up and down via several different actions) Use an event emitter! • Need to share state/data across a fairly big app? Use Flux. But Flux is typically overkill for most small-scale apps / widgets. • I wouldn’t recommend shoe-horning React into MV* frameworks like Angular or Backbone. It can work but I found it easier to stick to the idiomatic ways of approaching those MV*s. The Life Aquatic

Slide 21

Slide 21 text

Falcor Relay

Slide 22

Slide 22 text

J S X • JSX is not a required part of using React but (IMO) you’ll quickly regret not doing so. I don’t know anyone who doesn’t use some sort of language abstraction with React (ClojureScript, CoffeeScript, JSX, etc.) • In a nutshell, JSX lets you write markup (HTML) straight in your JavaScript. This always bothers everyone. It bothered me. Just trust me this far: start with it. It’s not hard to switch back to the pure JS implementation. • Babel (our ES6 transpiler) already has built in JSX support anyway. You would not be using it for the sake of not using it. A Clockwork Orange

Slide 23

Slide 23 text

M A S H I N G T O G E T H E R M A R K U P A N D J AVA S C R I P T S ? I S N ’ T T H AT A B A D I D E A ? lolwut Indiana Jones and the Temple of Doom

Slide 24

Slide 24 text

M U S H I N G T O G E T H E R C O N C E R N S • Being used to MVC from the server, a lot of us get uncomfortable at the idea of mushing together concerns. I certainly was. • What is good for the server is not good for the interface. Separating models out from controllers allows great code re-use for server. It’s a mess on the client. • Instead it makes sense to thinks of things in terms of components, which are made up of behaviors (or containers) and layouts. It often mimics your markup. 2001: A Space Odyssey

Slide 25

Slide 25 text

E C M A S C R I P T 6 • Latest release of the JavaScript language. Now in final draft. Wide adoption is beginning but very incomplete. • Babel is a transpiler that allows to write it now and have it translated to work on ES5 which is now widely adopted. You could have as easily used Tracer or another one. Babel just has JSX translation built in. • We’ll covering a small surface area of useful features in ES6 and specifically how it applies to React. React has new syntax that is specifically only for use with ES6. Django Unchained

Slide 26

Slide 26 text

T O O L I N G • We’ll be using a few noteworthy tools. Let’s briefly cover that: • Gulp – Simple build system. It’ll run our build. • Babel – We’ll be using it to transpile our ES6 JSX to ES5. • Browserify – We’ll use it to package our app together so it can be used in the browser. • Koa – An awesome node/iojs server that allows you to uses ES6 generators for middleware. If you’ve used Express it should feel very familiar. • node – JS on the server. I’ll be using v4.2.1 Star Wars Episode V: The Empire Strikes Back

Slide 27

Slide 27 text

D E M O Ghostbusters II github.com/btholt/es6-react-pres

Slide 28

Slide 28 text

Batman Begins state is bad lol @ H O LT B T B R I @ N H O . LT