Enhance your User (and Developer) Experience with React & Redux

A presentation at Web2Day 2018 in June 2018 in Nantes, France by Nicolas Goutay

Slide 1

Slide 1

Enhance your User (and Developer) Experience with React & Redux & /phacks

Slide 2

Slide 2

Hi! I am Nicolas Goutay . I work at Theodo , a web consultancy based in Paris & London. I build JS & Python web applications. I have stage fright am excited to be here with all of you ☺

You can find me online (Twitter & GitHub) on @phacks .

Slide 3

Slide 3

React & Redux A

Lego

analogy

Slide 4

Slide 4

test te fort veau jean jean humain loup travers des gout bon bonbon cher non How to build a F1 Lego tray Lego tray

Slide 5

Slide 5

React application code would be a Lego instruction manual, where bricks are DOM nodes . It takes care of how things look for the end user.

Slide 6

Slide 6

On modern Web apps, how things look are usually a function of user interactions . In this analogy, the user is Elya, my 5 year-old niece. Red is her favorite color, so she wants the car to be red " .

Slide 7

Slide 7

This is where

Redux kicks in.

Slide 8

Slide 8

$ “I want the car to be red.”

“Dispatch the CHANGE_CAR_COLOR

action with the payload color: red ” % “OK! I take note that you want your car red…”

“A reducer will process the action, and will add color: red to the Redux store ” % “…I sift through all the messy Legos to find red bricks…”

“The store passes the property

color: red to React components” % “…and I follow the instructions again with the red bricks”

Slide 9

Slide 9

test te fort veau jean jean humain loup travers des gout bon bonbon cher non How to build a F1 Redux Store color: red

Slide 10

Slide 10

Redux — Now with actual code export

function

changeCarColor ( color ) {

return { type: ' CHANGE_CAR_COLOR ',

color

} }

“Dispatch the CHANGE_CAR_COLOR

action with the payload color ”

Slide 11

Slide 11

Redux — Now with actual code function

formulaOneApp ( state = {}, action ) {

switch ( action .type) {

case ' CHANGE_CAR_COLOR ':

return

Object . assign ({}, state , { color: action .color })

default :

return

state

} }

“A reducer will process the action, and will add color to the Redux store ”

Slide 12

Slide 12

Redux — Now with actual code

“The store passes the properties

color

and

number to React components” import

React

from ' react ' const

FormulaOne = ({ color , number }) => ( < div

<

CarBody

color

{ color } /> < Decorations

number

{ number } /> </ div

)

Slide 13

Slide 13

Redux — The Redux Loop

Slide 14

Slide 14

Developer Experience — Easier Debugging

Slide 15

Slide 15

Developer Experience — Easier Debugging

Slide 16

Slide 16

Developer Experience — Easier Debugging This is what the Redux Store of my current project look like. I can inspect every variable, which are updated in real time.

Slide 17

Slide 17

User Experience — Built-in Performance

Slide 18

Slide 18

User Experience — Built-in Performance React components are only repainted when their props or inner state change. React API methods like shouldComponentUpdate

allow us to have a finer-grained control about render performance.

Slide 19

Slide 19

Developer Experience — “Reasonaboutability”

Slide 20

Slide 20

Developer Experience — “Reasonaboutability” React developer Jani Eväkallio coined the term “ reasonaboutability ” ( easiness to reason about ). I love it, and it matches perfectly what I feel about Redux. Here are the Three Principles of Redux:

Slide 21

Slide 21

Developer Experience — “Reasonaboutability” React developer Jani Eväkallio coined the term “ reasonaboutability ” ( easiness to reason about ). I love it, and it matches perfectly what I feel about Redux. Here are the Three Principles of Redux: Single Source of Truth : all the data/UI state displayed on the app come from the same JS object. Facilitates debugging.

Slide 22

Slide 22

Developer Experience — “Reasonaboutability” React developer Jani Eväkallio coined the term “ reasonaboutability ” ( easiness to reason about ). I love it, and it matches perfectly what I feel about Redux. Here are the Three Principles of Redux: Single Source of Truth : all the data/UI state displayed on the app come from the same JS object. Facilitates debugging. State is read-only : The only way to change the state is to emit an action, an object describing what happened. Provides a single, robust & semantic way to deal with interactions and to work as a team.

Slide 23

Slide 23

Developer Experience — “Reasonaboutability” React developer Jani Eväkallio coined the term “ reasonaboutability ” ( easiness to reason about ). I love it, and it matches perfectly what I feel about Redux. Here are the Three Principles of Redux: Single Source of Truth : all the data/UI state displayed on the app come from the same JS object. Facilitates debugging. State is read-only : The only way to change the state is to emit an action, an object describing what happened. Provides a single, robust & semantic way to deal with interactions and to work as a team. Changes are made with pure functions : the Store can only be updated with pure functions (reducers). Prevents nasty side effects and facilitates testing.

Slide 24

Slide 24

Redux comes with other benefits — and tradeoffs

Slide 25

Slide 25

&

Rich ecosystem : Redux has an API to plug-in middlewares . There are tons of them: for logging, offline capabilities, async, forms, optimistic UIs… Tip:

'

github.com/markerikson/react-redux-links is a great place to start!

Slide 26

Slide 26

(

Code structure is key : Since all UI is derived from a single JS object, it needs to be carefully designed and constantly adjusted to business requirements. Tip: Learn from the best! Twitter & Pinterest both use Redux, and the structure is available for anybody to see with the React Dev Tools!

Slide 27

Slide 27

Tip: I just got used to it. After a while it even feels more productive

than Angular 1, because you know exactly what to do to get everything to work together. )

Verbosity : to write a feature, you would usually need to write an action, a reducer, a selector , a saga … It can feel quite cumbersome compared to Angular 1.

Slide 28

Slide 28

“So, should I use Redux?”

Slide 29

Slide 29

Slide 30

Slide 30

The key points “If you’re just learning React, don’t make Redux your first choice” For personal side projects , very small teams (1-2 people) or MVPs with very short time to market, drop Redux & go React For long-running projects , or larger teams , Redux will help you work better together and lead to a more maintainable code base

Slide 31

Slide 31

Conclusion React with Redux is a mature framework (React just turned five! * ) that empowers developers to produce performant apps with facilitated debugging and a standard yet expressive development flow.

Slide 32

Slide 32

Want to dive in? I wrote a full-featured, test-driven tutorial on writing a Todo List using React & Redux

'

https://github.com/phacks/redux-todomvc

Slide 33

Slide 33

Merci! Slides are available at phacks.github.io & /phacks