Top Banner
REDUX A (better) way to manage app state
26

Redux tutorial - intro to Redux by GetLittleTech

Jan 21, 2017

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Redux tutorial - intro to Redux by GetLittleTech

REDUXA (better) way to manage app state

Page 2: Redux tutorial - intro to Redux by GetLittleTech

WHY REDUX?• There are other frameworks - Angular, Knockout, Backbone

• Most - bidirectional data flow

• Can introduce circular dependencies

• State can become unpredictable

• Change a small thing and break everything

• Flux and Redux came up - unidirectional data flow

Page 3: Redux tutorial - intro to Redux by GetLittleTech

UNIDIRECTIONAL FLOW

Redux - an implementation of FLUX

Flux diagram:

Page 4: Redux tutorial - intro to Redux by GetLittleTech

DEFINE SOME TERMS• State

• Actions and Action creators

• Reducers

• Store

• …

Page 5: Redux tutorial - intro to Redux by GetLittleTech

BEFORE WE BEGIN

• EcmaScript 6 (ES6)

• Babel - to use ES6 in the browsers

• NPM - yes, for frontend dependencies

• Webpack or Browserify

Page 6: Redux tutorial - intro to Redux by GetLittleTech

NOW, LET’S DEFINE THOSE TERMS

Page 7: Redux tutorial - intro to Redux by GetLittleTech

STATE• All the data your app has

{

messages: [

{title: “Congrats!”, from: “Oleg”, to: “Peter”, videoUrl: “https://super.video.mo”},

{…}, {…}],

visibilityFilter: ‘NEW’,

… ?

}

Page 8: Redux tutorial - intro to Redux by GetLittleTech

ACTIONS• Describe new pieces of state

• Plain objects

• Should have a type

{

type: “UPDATE_MESSAGE”,

messageUpdate: {

id: “87784929384833”,

title: “Congrats again!”

}

}

Page 9: Redux tutorial - intro to Redux by GetLittleTech

ACTION CREATORS• Helper functions, that create/return actions

export function updateMessage(messageUpdate) {

return {

type: ‘UPDATE_MESSAGE’,

messageUpdate

}

}

Page 10: Redux tutorial - intro to Redux by GetLittleTech

EASY SO FAR?

Page 11: Redux tutorial - intro to Redux by GetLittleTech

REDUCERS• Pure functions

• Does not modify variables outside of its scope

• Always return the same result given the same arguments

• Action and previous state as parameters

• Return the new state

function messages(previousState, action) {

var newState = … // Do not mutate the previous state. Create a new one.

return newState

}

Page 12: Redux tutorial - intro to Redux by GetLittleTech

LET’S TRY IT

previousState = { messages: [ ] }

action = { type: ‘ADD_MESSAGE’, message: { title: ‘Congrats!’ } }

What should be our new state?

What should our Reducer look like?

Page 13: Redux tutorial - intro to Redux by GetLittleTech

AND THE ANSWER IS..

Page 14: Redux tutorial - intro to Redux by GetLittleTech

THE “RIGHT” ANSWERstate = {

messages: [ {title: ‘Congrats!’} ] }

function messages(previousState, action) {

switch (action.type) {

case ‘ADD_MESSAGE’:

// Do not mutate the previous state. Create a new one.

return Object.assign({}, previousState,

{messages: [...previousState.messages, action.message]}

)

// cases for REMOVE_MESSAGE, EDIT_MESSAGE, etc..

default:

return previousState

}

}

Page 15: Redux tutorial - intro to Redux by GetLittleTech

REDUCER COMPOSITION• We want to “combine” different reducers:

// state { messages: […], visibilityFilter: ‘’, settings }

function mainBigSuperReducer(state = {}, action) { return { messages: messages(state.messages, action), visibilityFilter: visibilityFilter(state.visibilityFilter, action), settings: settings(state.settings, action) } }

Page 16: Redux tutorial - intro to Redux by GetLittleTech

WHAT WE HAVE:

• State

• Actions

• Reducers

Page 17: Redux tutorial - intro to Redux by GetLittleTech

STORE• Brings everything together:

• holds state

• allows to dispatch actions (the only way to change the store)

• applies reducers to the actions

• allows listeners to subscribe

let store = createStore(mainBigSuperReducer)

Page 18: Redux tutorial - intro to Redux by GetLittleTech

NOW YOU ARE CLOSE TO KNOWING REDUX

Page 19: Redux tutorial - intro to Redux by GetLittleTech

REDUX MAIN PRINCIPLES

1. State is stored in an object tree with a single store

2. State is read-only

3. Change is made via reducers (triggered by dispatching an action)

Page 20: Redux tutorial - intro to Redux by GetLittleTech

STORE AND DATA FLOW

Add Message store.dispatch(addMessage(params))

ACTION: type: ‘ADD_MESSAGE’ message: { title: params.title }

STORE:currentStatemainReducerlisteners

1. get new current state:

currentState = mainReducer(currentState, ACTION)

2. notify listeners

Page 21: Redux tutorial - intro to Redux by GetLittleTech

WHAT ARE THE LISTENERS?

Page 22: Redux tutorial - intro to Redux by GetLittleTech

REACT COMES IN

Add Message store.dispatch(addMessage(params))

ACTION: type: ‘ADD_MESSAGE’ message: { title: params.title }

STORE:currentStatemainReducerlisteners

1. get new current state:

currentState = mainReducer(currentState, ACTION)

2. notify listeners

React

connect(…)

handleChange()

react-redux

(once in the beginning)

Page 23: Redux tutorial - intro to Redux by GetLittleTech

FLUX DIAGRAM MAKES SENSE NOW

Page 24: Redux tutorial - intro to Redux by GetLittleTech

5 HOURS IN 1 SLIDE

Add Message store.dispatch(addMessage(params))

ACTION: type: ‘ADD_MESSAGE’ message: { title: params.title }

STORE:currentStatemainReducerlisteners

1. get new current state:

currentState = mainReducer(currentState, ACTION)

2. notify listeners

React

connect(…)

handleChange()

react-redux

(once in the beginning)

Page 25: Redux tutorial - intro to Redux by GetLittleTech

SO MUCH LEFT…

• More on React

• Async actions and middlewear

• Server-side rendering

• DevTools and time-travel/replay

Page 26: Redux tutorial - intro to Redux by GetLittleTech

DISCUSSION