better react state management with redux

78
Better React state management with Redux Maurice de Beijer @mauricedb @react_tutorial

Upload: maurice-beijer

Post on 05-Apr-2017

96 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Better React state management with Redux

Better React state management with ReduxMaurice de Beijer@mauricedb@react_tutorial

Page 2: Better React state management with Redux

© ABL - The Problem Solver 2

Workshop goal

Short recap: what is React? Learn the three principals of Redux Use the different Redux components Unit testing Redux code Adding Redux to a React application Use Redux middleware

Page 3: Better React state management with Redux

3

Maurice de Beijer The Problem Solver Microsoft Azure MVP Freelance developer/instructor Twitter: @mauricedb and @React_Tutorial Web: http://www.TheProblemSolver.nl E-mail: [email protected]

Page 4: Better React state management with Redux

© ABL - The Problem Solver 4

Follow along

Git repository: https://github.com/mauricedb/nitflex-redux

Slides: http://bit.ly/react-redux-workshop

Page 5: Better React state management with Redux

© ABL - The Problem Solver 5

Type it outby hand?

“Typing it drills it into your brain much better than

simply copying and pasting it. You're forming new neuron pathways. Those pathways are going to help you in the future. Help them out now!”

Page 6: Better React state management with Redux

© ABL - The Problem Solver 6

PrerequisitesInstall Node & NPM

Page 7: Better React state management with Redux

© ABL - The Problem Solver 7

Install Node.js &

NPM

Page 8: Better React state management with Redux

© ABL - The Problem Solver 8

Short recapWhat is React?

Page 9: Better React state management with Redux

© ABL - The Problem Solver 9

React“React is a JavaScript library for building user interfaces”

- Facebook -

Page 10: Better React state management with Redux

© ABL - The Problem Solver 10

React features

React uses a component based architecture

Components are written using the JSX syntax

React likes a one way data flow React uses a virtual DOM

Page 11: Better React state management with Redux

© ABL - The Problem Solver 11

React Componen

t

Page 12: Better React state management with Redux

© ABL - The Problem Solver 12

NitflexThe sample application

Page 13: Better React state management with Redux

© ABL - The Problem Solver 13

Nitflex

Page 14: Better React state management with Redux

© ABL - The Problem Solver 14

Main componen

ts

Application

Login Main Page

Header Billboard Genre List

Genre*

Movie* Expanded Movie

Playing Movie

Page 15: Better React state management with Redux

© ABL - The Problem Solver 15

Stateful componen

ts

Application

Login Main Page

Header Billboard Genre List

Genre*

Movie* Expanded Movie

Playing Movie

Page 16: Better React state management with Redux

© ABL - The Problem Solver 16

Clone Nitflex

&NPM install

Page 17: Better React state management with Redux

© ABL - The Problem Solver 17

Redux

Page 18: Better React state management with Redux

© ABL - The Problem Solver 18

Redux

“Redux is a predictable state container for JavaScript

apps”

- Dan Abramov -

Page 19: Better React state management with Redux

© ABL - The Problem Solver 19

Three principals of Redux

Single source of truth State is read-only Changes are made with pure functions

Page 20: Better React state management with Redux

© ABL - The Problem Solver 20

Single source of

truth

The application state is stored in a single location

Do not scatter the state over lots of components

A single state location makes it easier to reason about and debug your application

Page 21: Better React state management with Redux

© ABL - The Problem Solver 21

State isread-only

The state objects are never changed by the application

Every state change is the result of an action being dispatched to the store

This action describes the intended state change

Page 22: Better React state management with Redux

© ABL - The Problem Solver 22

Changes with pure functions

Reducers are pure functions that take the current state and an action as input and return a new state object

The previous state object is never modified

Page 23: Better React state management with Redux

© ABL - The Problem Solver 23

UI versus App state

Redux should be used for application state.

Do not store UI state in Redux!

Page 24: Better React state management with Redux

© ABL - The Problem Solver 24

Login example

Page 25: Better React state management with Redux

© ABL - The Problem Solver 25

InstallingRedux

Page 26: Better React state management with Redux

© ABL - The Problem Solver 26

Redux Components

Page 27: Better React state management with Redux

© ABL - The Problem Solver 27

Redux Componen

ts

Actions Reducers The store

Page 31: Better React state management with Redux

© ABL - The Problem Solver 31

Unit testing

Page 32: Better React state management with Redux

© ABL - The Problem Solver 32

Unit test the action

creator

Page 34: Better React state management with Redux

© ABL - The Problem Solver 34

Redux and React

Page 35: Better React state management with Redux

© ABL - The Problem Solver 35

Redux and React

The NPM react-redux package contains the React bindings for Redux

The <Provider> component makes the Redux store available

The connect() function connects React components to the Redux store use in <Provider>

Page 37: Better React state management with Redux

© ABL - The Problem Solver 37

Dispatch login

action

Page 38: Better React state management with Redux

© ABL - The Problem Solver 38

Get the user from the store

Page 39: Better React state management with Redux

© ABL - The Problem Solver 39

Cleanup app-container.jsx

Remove user and loginAsUser() app-presentation.jsx

Remove loginAsUser()

Page 40: Better React state management with Redux

© ABL - The Problem Solver 40

Redux Middleware

Page 41: Better React state management with Redux

© ABL - The Problem Solver 41

Redux Middlewar

e

“Redux middleware provides a third-party extension point

between dispatching an action, and the moment it

reaches the reducer”

- Dan Abramov -

Page 42: Better React state management with Redux

© ABL - The Problem Solver 42

Installing redux-thunk

Page 43: Better React state management with Redux

© ABL - The Problem Solver 43

Redux-Thunk

Page 44: Better React state management with Redux

© ABL - The Problem Solver 44

What is a Thunk?

“A thunk is a subroutine that is created, often

automatically, to assist a call to another subroutine”

- Wikipedia -

Page 45: Better React state management with Redux

© ABL - The Problem Solver 45

Configure Thunk

Middleware

Page 48: Better React state management with Redux

© ABL - The Problem Solver 48

Update the

reducers index.js

Page 49: Better React state management with Redux

© ABL - The Problem Solver 49

Extract movies from store

Page 50: Better React state management with Redux

© ABL - The Problem Solver 50

Cleanup app-container.jsx

Remove allMovies and passing movies to AppPresentation

Page 51: Better React state management with Redux

© ABL - The Problem Solver 51

Redux DevTools

Page 52: Better React state management with Redux

© ABL - The Problem Solver 52

Redux DevTools

Page 53: Better React state management with Redux

© ABL - The Problem Solver 53

Redux DevTools

Page 54: Better React state management with Redux

© ABL - The Problem Solver 54

Remember the user

Page 55: Better React state management with Redux

© ABL - The Problem Solver 55

Remember the user

Page 56: Better React state management with Redux

© ABL - The Problem Solver 56

Playing and stopping movies

Page 60: Better React state management with Redux

© ABL - The Problem Solver 60

Retrieve the

playing movie

from the store in

app-presentati

on

Page 61: Better React state management with Redux

© ABL - The Problem Solver 61

Retrieve the

playing movie

from the store in playing-movie

Page 62: Better React state management with Redux

© ABL - The Problem Solver 62

Cleanup app-container.jsx main-page/index.jsx genre-list.jsx genre-row.jsx

Page 63: Better React state management with Redux

© ABL - The Problem Solver 63

Filtering movies

Page 64: Better React state management with Redux

© ABL - The Problem Solver 64

Filtering movies

The list of filtered movies are derived from state

Page 69: Better React state management with Redux

© ABL - The Problem Solver 69

Cleanup Header.jsx

Remove filterMovies Main-page/index.jsx

Remove filterMovies

Page 70: Better React state management with Redux

© ABL - The Problem Solver 70

Removeapp-container

The AppContainer is no longer needed.

Page 72: Better React state management with Redux

© ABL - The Problem Solver 72

Connect only to required movie data

Page 77: Better React state management with Redux

© ABL - The Problem Solver 77

Bonus Exercises

Extract updating localStorage from user reducer and do this with middleware

The user is passed from AppPresentation => MainPage => Header