首页 > 移动平台 > 详细

[Redux] Wrapping dispatch() to Log Actions

时间:2016-06-07 06:34:58      阅读:276      评论:0      收藏:0      [点我收藏+]

We will learn how centralized updates in Redux let us log every state change to the console along with the action that caused it.

 

import { createStore } from ‘redux‘;
import throttle from ‘lodash/throttle‘;
import todoApp from ‘./reducers‘;
import { loadState, saveState } from ‘./localStorge‘;

const addLoggingToDispatch = (store) => {

    const rawDispatch = store.dispatch;

    // If browser not support console.group
    if (!console.group) {
        return rawDispatch;
    }

    return (action) => {
        console.group(action.type);
        console.log(‘%c prev state‘, ‘color: gray‘, store.getState());
        console.log(‘%c action‘, ‘color: blue‘, action);
        const returnValue = rawDispatch(action);
        console.log(‘%c next state‘, ‘color: green‘, store.getState());
        console.groupEnd(action.type);
        return returnValue;
    };
};

const configureStore = () => {
    const persistedState = loadState();
    const store = createStore(todoApp, persistedState);

    // If in production do not log it
    if (process.env.NODE_ENV !== ‘production‘) {
        store.dispatch = addLoggingToDispatch(store);
    }

    store.subscribe(throttle(() => {
        saveState({
            todos: store.getState().todos,
        });
    }, 1000));

    return store;
};

export default configureStore;

 

技术分享

[Redux] Wrapping dispatch() to Log Actions

原文:http://www.cnblogs.com/Answer1215/p/5565718.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!