You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Encourage 'pure' functions (action creators) and good functional programming (composition)
Thunks, Sagas, Epics - Redux Middleware
Allows asynchronous actions in Redux
Thunks - returns functions
Sagas - returns function generators
Epics - returns functions that return observables
Thunks
Create an action creator that returns a function(dispatch,getState)
Chain dispatches and promises, such as calls to the server
Maintain synchronous flow through your Redux app
Example:
functionmakeSandwichesForEverybody(){returnfunction(dispatch,getState){// We can dispatch both plain object actions and other thunks,// which lets us compose the asynchronous actions in a single flow.returnPromise.all([dispatch(makeASandwichWithSecretSauce('Me')),dispatch(makeASandwichWithSecretSauce('Friend'))]).then(()=>dispatch(makeASandwichWithSecretSauce('Our neighbors'))).then(()=>dispatch(getState().myMoney>42 ?
withdrawMoney(42) :
apologize('Me','The Sandwich Shop')));};}
Sagas
Create an action creator that returns a function generator
Like thunks, we can handle complicated asynchronous sequences, but in "generator" style
Sagas allow a cleaner way, than Thunks, to handle side effects and errors, and return the original state
Epics
Create an action creator that returns an observable of actions
Observable Example:
conststartTicking=(actions,store)=>Observable.interval(1000).map((i)=>({type: 'TICK', i })).takeUntil(actions.ofType('STOP_TICK'));dispatch(startTicking);// to stop the ticking actions at a later pointdispatch({type: 'STOP_TICK'});
Like thunks and sagas, we can handle complicated asynchronous sequences, but in "observable" style
Epics, like Sagas, allow a cleaner way to handle side effects and errors, and return the original state