workshop 22: react-redux middleware

29
Front End Workshops Redux Advanced Enrique Oriol [email protected] Alex Adrià [email protected]

Upload: visual-engineering

Post on 23-Jan-2018

964 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Workshop 22: React-Redux Middleware

Front End Workshops

Redux Advanced

Enrique [email protected]

Alex Adrià[email protected]

Page 2: Workshop 22: React-Redux Middleware
Page 3: Workshop 22: React-Redux Middleware

Previously in ReactJS...

ReactJS Basics

1. Decompose UI into reusable components which render to virtual DOM

2. ReactJS will sync VDOM to real browser DOM

3. JSX format

4. Can render on client and server

Page 5: Workshop 22: React-Redux Middleware

Previously in ReactJS...

Redux

React Action Creator

Action

Reducers

State

Calls Returns

Sent to

Creates new

Sent to

Page 6: Workshop 22: React-Redux Middleware

Previously in ReactJS...

React-router

React

History

React router

Page 7: Workshop 22: React-Redux Middleware

Higher Order Components(HOC)

Page 8: Workshop 22: React-Redux Middleware

What is a HOC?

● It is a JavaScript function which is used for adding additional functionality to existing component

● A takes an existing component and returns another component that wraps it.

● If the HOC data changes, It will re-run again and update the resulting component.

● It usually contains reusable common behaviours resulting in simpler and better structured components.

● A component can be wrapped several times by multiple wrappers.

Page 9: Workshop 22: React-Redux Middleware

Decorator Pattern

Page 10: Workshop 22: React-Redux Middleware

Decorator Pattern

Page 11: Workshop 22: React-Redux Middleware

export default function(ComposedComponent) {

class HOCExample extends Component {

componentDidMount() {

this.setState({data:'Data from HOC...'});

}

render() {

return <ComposedComponent {...this.props} {...this.state} />;

}

}

return HOCExample;

}

HOC structure example

Page 12: Workshop 22: React-Redux Middleware

Redux Middleware

Page 13: Workshop 22: React-Redux Middleware

What is middleware

Provides capability to

put CODE

between

dispatching an action

and

reaching the reducer.

Page 14: Workshop 22: React-Redux Middleware

Basic redux life-cycle

React Action Creator

Action

Reducers

State

Calls Returns

Sent to

Creates new

Sent to

Page 15: Workshop 22: React-Redux Middleware

Redux life-cycle with middlewares

Middleware

React Action Creator

Action

Reducers

State

Calls Returns

Sent to

Forwards action to

Creates new

Sent to Middleware receives the action

and can log stop and modify it

Page 16: Workshop 22: React-Redux Middleware

Middleware benefits

COMPOSABLE

INDEPENDENT

Page 17: Workshop 22: React-Redux Middleware

Middleware stack example 1

Middleware # 1

I don’t care about this action, I’ll send it to the NEXT middleware

Action Creator Action

Reducers

nextMiddleware # 2

I will log this action, and move it forward

Middleware # 3

I don’t care about this action, I’ll send it to the NEXT middleware

next

console.log()

Page 18: Workshop 22: React-Redux Middleware

Middleware structure

● It is a function that receives the store

● It MUST return a function with arg “next”

● That returns a function with arg “action”

○ Where we do our stuff○ And return

■ next(action)■ state.dispatch(action)

Page 19: Workshop 22: React-Redux Middleware

export default function(store){

return function(next){

return function(action){

//do something

//use next(action) or

//state.dispatch(action)

}

}

}

Middleware structure

ES 5 middleware declaration

Page 20: Workshop 22: React-Redux Middleware

export default store => next => action => {

//do something

//next(action); or state.dispatch(action);

}

}

Middleware structure ES6

export default ({dispatch}) => next => action => {

//our stuff

}

}

src/middleware/myMiddleware.js

In case we don’t need the store

Page 21: Workshop 22: React-Redux Middleware

Middleware

C’MON MAN!

GIVE ME an EXAMPLE!!

Page 22: Workshop 22: React-Redux Middleware

Simplest example - logger

Middleware

REACT IncreaseCounter() INC_ACTION

ReducersSTATE- counter

Forwards action to

Component

counter+

- DecreaseCounter()

DEC_ACTION

Update state

counter

logger

Page 23: Workshop 22: React-Redux Middleware

Using our middleware

import {createStore, applyMiddleware } from 'redux';

import reducers from './reducers';

import MyMid from './middlewares/my-middleware';

const createStoreWithMiddleware = applyMiddleware(myMid)(createStore);

ReactDOM.render(

<Provider store={createStoreWithMiddleware(reducers)}>

<App />

</Provider>

, document.querySelector('.container'));

src/index.js

Page 24: Workshop 22: React-Redux Middleware

Modify action middleware workflow

Middleware that modifies specific action

Is it the action that I want?

Action Creator Action

no yes

Send action forwards

Next middleware

Do some stuff

Create new action with the results Action

Middlewarestack

next(action)store.dispatch(newAction)

Page 25: Workshop 22: React-Redux Middleware

Middleware

CAN YOU

SHOW ME

SOME CODE?

Page 26: Workshop 22: React-Redux Middleware

Dispatch action example - superstitious counter

Middleware

REACT IncreaseCounter() INC_ACTION

ReducersSTATE- counter

Forwards action to

Component

counter+

- DecreaseCounter()

DEC_ACTION

Update state

counter

logger

superstitious

Page 27: Workshop 22: React-Redux Middleware

Popular middlewares

redux-promise

redux-thunk

Redux-logger

Remember: npm install --save package

Page 28: Workshop 22: React-Redux Middleware

Thanks for your time!

Do you have any questions?

Page 29: Workshop 22: React-Redux Middleware