How to Redux

18
How to Redux Ted Pennings New Relic Senior Software Engineer

Transcript of How to Redux

Page 1: How to Redux

How to ReduxTed Pennings

New Relic Senior Software Engineer

Page 2: How to Redux

What is Redux?• Broadly, a flux implementation

• Simple, conceptually and in file size (2kb)

• Inspired by functional programming and immutability (Elm, Clojure)

• Powers new APM Errors feature!

• Written by a super brilliant dude, Dan Abramov

• https://github.com/rackt/redux and http://redux.js.org/

Page 3: How to Redux

Why Redux?

• Predictable state transitions

• Single source of truth for UI state

• Highly performant

• Easily testable

Page 4: How to Redux

Why Redux?

👍Predictable state transitions

👍Single source of truth for UI state

👍Highly performant

👍Easily testable

Page 5: How to Redux

The core concepts

Store

Actions and action creators

Reducers

Middleware

Page 6: How to Redux

Store

• A wrapper around a Javascript object (state)

• Two key methods: getState(), dispatch()

• Using something like react-redux, you don’t really touch this directly!

▹ Store Actions Reducers Middleware

Page 7: How to Redux

Store▹ Store

Actions Reducers Middleware

Disclaimer: not real code from APM product. Everything else is 💃

Page 8: How to Redux

Actions• At its core, it’s a simple Javascript object

• type is the only required field (use constants)

• payload generally carries your data

• Action creators: functions that return the body of an action

• Happy place:“flux standard actions” spec

☑ Store ▹ Actions

Reducers Middleware

Page 9: How to Redux

Actions☑ Store ▹ Actions

Reducers Middleware

Page 10: How to Redux

Reducers• function reduce (state, action) { return state + 1}

• Generally a medium-size switch statement on action.type

• MUST return new copies of state that reflect changes

• Happy place: combineReducers(), immutability helpers

☑ Store ☑ Actions ▹ Reducers

Middleware

Page 11: How to Redux

Reducers☑ Store ☑ Actions ▹ Reducers

Middleware

Page 12: How to Redux

Reducers☑ Store ☑ Actions ▹ Reducers

Middleware

Page 13: How to Redux

Middleware• Meta-actions

• Middleware can prevent actions from reaching the store’s reducers

• Middleware can dispatch actions (eg, dispatch a ‘better’ action in response to an action)

• Recommended: thunk, redux-promise, batched-updates

• We haven’t needed to write our own Redux middleware at New Relic (happy place = nothing to do)

☑ Store ☑ Actions ☑ Reducers ▹ Middleware

Page 14: How to Redux

So… how do I use it?

Redux is built on many (great) opinions, so let’s ask that in reverse

Page 15: How to Redux

So… how don’t I use it?💥NEVER MODIFY STATE IN THE STORE DIRECTLY

💥REDUCERS MUST BE FREE OF SIDE EFFECTS (NEVER MODIFY STATE DIRECTLY)

💥NEVER CHANGE DATA DIRECTLY. Redux does this for you as reducers return new copies of state that apply actions

💥 Trivial things like mouse/hover position or checkbox-type state doesn’t belong in Redux, I’d argue.

Page 16: How to Redux

So… how do I use it?• In React: use

redux-react and connect()’ed React classes (but connect higher-level containers only)

• Redux has Angular bindings

Page 17: How to Redux

So… how should I use it?• Use two classes of actions:

• Lower-level simple actions for simple state changes

• Higher-level actions (redux-thunk) to coordinate interactions

• Happy place: idempotent actions, self-throttling

Page 18: How to Redux

Let’s build great things!😂Redux is powerful for building predictable,

testable, maintainable interfaces.

😂Redux makes you a better UI engineer by forcing you to handle state changes explicitly.

😂 I love talking about Redux, immutable state and functional programming in the UI tier. Let’s geek out about it! ( @thesleepyvegan)

😂 Thank you! 🍩