provjs: six months of reactjs and redux

40
SIX MONTHS OF REACT & REDUX: A REAL WORLD USE CASE Thom Nichols - @thom_nic

Upload: thom-nichols

Post on 06-Jan-2017

378 views

Category:

Software


0 download

TRANSCRIPT

Page 1: ProvJS:  Six Months of ReactJS and Redux

SIX MONTHS OF REACT & REDUX:A REAL WORLD USE CASEThom Nichols - @thom_nic

Page 2: ProvJS:  Six Months of ReactJS and Redux

DISCLAIMER:

Not an expert I might be doing it wrong

Page 3: ProvJS:  Six Months of ReactJS and Redux

SO YOU WANT TO TRY REACT + REDUX…

Page 4: ProvJS:  Six Months of ReactJS and Redux

WHAT ARE YOU (PROBABLY) GOING TO NEED?

Webpack Babel + presets

React-redux Redux-thunk Redux-logger Axios or fetch-polyfill Immutable.js

React Redux

Jest or Mocha + JSDOM or Karma + Phantomjs React-router React-router-redux

Page 5: ProvJS:  Six Months of ReactJS and Redux

ALL YOU NEED IS A FEW PACKAGES FROM NPM

Page 6: ProvJS:  Six Months of ReactJS and Redux

GREAT! AM I READY TO START PROGRAMMING NOW?

Page 7: ProvJS:  Six Months of ReactJS and Redux

WHAT ARE YOU (PROBABLY) GOING TO NEED?

Webpack Babel + presets

React-redux Redux-thunk Redux-logger Axios or fetch-polyfill Immutable.js

React Redux

Jest or Mocha + JSDOM or Karma + Phantomjs React-router React-router-redux

What we actually care about

Page 8: ProvJS:  Six Months of ReactJS and Redux

WHAT ARE YOU (PROBABLY) GOING TO NEED?

Webpack Babel + presets

React-redux Redux-thunk Redux-logger Axios or fetch-polyfill Immutable.js

React Redux

Jest or Mocha + JSDOM or Karma + Phantomjs React-router React-router-redux

What we actually care about

Page 9: ProvJS:  Six Months of ReactJS and Redux

LET’S TALK ABOUT REDUX

Page 10: ProvJS:  Six Months of ReactJS and Redux

FLUX ARCHITECTURE

Page 11: ProvJS:  Six Months of ReactJS and Redux

FLUX ARCHITECTURE - SIMPLIFIED

Page 12: ProvJS:  Six Months of ReactJS and Redux

SHOW ME THE CODE!

Page 13: ProvJS:  Six Months of ReactJS and Redux

TRIVIAL ACTION EXAMPLEstore.dispatch( addTodo(‘call mom’) );

Page 14: ProvJS:  Six Months of ReactJS and Redux

TRIVIAL REDUCER EXAMPLE

Page 15: ProvJS:  Six Months of ReactJS and Redux

THINGS NOBODY TOLD ME ABOUT REACT + REDUX

Page 16: ProvJS:  Six Months of ReactJS and Redux

USE IMMUTABLE.JS RECORDS! Without Records:

With Records:

Page 17: ProvJS:  Six Months of ReactJS and Redux

USE IMMUTABLE.JS RECORDS!

Page 18: ProvJS:  Six Months of ReactJS and Redux

WHAT SHOULD MY STORES HOLD?

Page 19: ProvJS:  Six Months of ReactJS and Redux

WHAT DOES MY DATA LOOK LIKE?

Page 20: ProvJS:  Six Months of ReactJS and Redux

MY STORES HOLD MAPS

Page 21: ProvJS:  Six Months of ReactJS and Redux

STORES MAY LISTEN TO “OTHER” ACTIONS

Page 22: ProvJS:  Six Months of ReactJS and Redux

I’M IN UR COMPONENT…

CONNECTIN UR PROPS

Page 23: ProvJS:  Six Months of ReactJS and Redux

ACTIONS + PROMISE CHAINING = AWESOME

Page 24: ProvJS:  Six Months of ReactJS and Redux

ACTIONS + PROMISE CHAINING = AWESOME

Page 25: ProvJS:  Six Months of ReactJS and Redux

WHAT’S ANNOYING? Component testing connected components is hard.

I rely (mostly) on functional testing

Export both a connect()-ed and non-connected component See: Erik Ras’ “ducks”

Page 26: ProvJS:  Six Months of ReactJS and Redux

WHAT’S ANNOYING? It’s difficult to call a method on a component wrapped as a

high-order component…

Page 27: ProvJS:  Six Months of ReactJS and Redux

DON’T DO IT BUT: you probably shouldn’t be doing that anyway:

pass data via callbacks passed from parent component. Or pass data via a redux action

Page 28: ProvJS:  Six Months of ReactJS and Redux

LET’S RECAP Use ImmutableJS records for domain types in stores Use ImmutableJS Maps often for store data containers Use redux-thunk to make remote requests in actions Async action creators should always return a promise Reducer cases are not always 1-to-1 with actions Let me know if you’ve figured out the component testing issue

with connected components… HoC component wrapping cause some pain Webpack is really the most annoying part

Page 29: ProvJS:  Six Months of ReactJS and Redux

MORE TRICKS

Page 30: ProvJS:  Six Months of ReactJS and Redux

DEBOUNCE EXPENSIVE ACTIONS

Some independent top-level components say what data they want to fetch from `componentWillMount()`

Debounce a single remote request

Page 31: ProvJS:  Six Months of ReactJS and Redux

CONSISTENTLY DISPATCH START & END ACTIONS

These are used for loading indicator & error messaging

Page 32: ProvJS:  Six Months of ReactJS and Redux

BONUS: REDUX-FORM IS AWESOME

Page 33: ProvJS:  Six Months of ReactJS and Redux

BONUS: REDUX-FORM IS AWESOME

Page 34: ProvJS:  Six Months of ReactJS and Redux

DECIDE WHAT WORKS FOR YOU

Frameworks are less about prescribing “the right way” and more about getting everyone to do things the same way.

- @thom_nic

Page 35: ProvJS:  Six Months of ReactJS and Redux

THANKS

Page 36: ProvJS:  Six Months of ReactJS and Redux

ACTUALLY….

ASK ME ABOUT DIGITAL ELECTRICITY

Page 37: ProvJS:  Six Months of ReactJS and Redux

MORE SLIDES

Page 38: ProvJS:  Six Months of ReactJS and Redux

I DON’T NEED ANGULAR-RESOURCE

This is actually (really) boilerplate. Replaced with this:

Page 39: ProvJS:  Six Months of ReactJS and Redux

WHY I DON’T NEED ANGULAR-RESOURCE … and http thunks of thunks:

Page 40: ProvJS:  Six Months of ReactJS and Redux

A WORD TO THE WISE…

Avoid temptation to do client-side data manipulation to attempt to mirror server state.

Better to always fetch from the server as the source of truth