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
import{createStore,applyMiddleware}from'redux';importtapfrom'redux-tap';importrootReducerfrom'./reducers';// For example, select any action with metadata:constselectMeta=action=>action.meta;// Once selected, access the selected value, action and store:constmiddleware=tap(selectMeta,(meta,action,store)=>{// In this case, we'll simply log to the console:console.log(meta,action,store);});// Note: this API requires redux@>=3.1.0conststore=createStore(rootReducer,applyMiddleware(middleware));
As a real-world example, you can use redux-tap to declaratively track analytics events:
import{createStore,applyMiddleware}from'redux';importtapfrom'redux-tap';importrootReducerfrom'./reducers';import{track}from'my-analytics-lib';constselectAnalytics=({ meta })=>meta&&meta.analytics;constmiddleware=tap(selectAnalytics,({ event, data })=>{track(event,data);});conststore=createStore(rootReducer,applyMiddleware(middleware));// Now, you can declare analytics metadata on any action:dispatch({type: 'REPO_STARRED',payload: { id },meta: {analytics: {event: 'Repo Starred',data: { id }}}});