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的更多相关文章

  1. redux & multi dispatch & async await

    redux & multi dispatch & async await 同时发送多个 action, 怎么保证按序返回数据 dispatch multi actions http:/ ...

  2. [Redux] Accessing Dispatch and State with Redux -- connect

    If you have props and actions, you want one component to access those props and actions, one solutio ...

  3. webpack+react+redux+es6开发模式

    一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...

  4. webpack+react+redux+es6

    一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...

  5. [React + Functional Programming ADT] Create Redux Middleware to Dispatch Actions with the Async ADT

    We would like the ability to group a series of actions to be dispatched with single dispatching func ...

  6. redux+flux(一:入门篇)

    React是facebook推出的js框架,React 本身只涉及UI层,如果搭建大型应用,必须搭配一个前端框架.也就是说,你至少要学两样东西,才能基本满足需要:React + 前端框架. Faceb ...

  7. 通过Redux源码学习基础概念一:简单例子入门

    最近公司有个项目使用react+redux来做前端部分的实现,正好有机会学习一下redux,也和小伙伴们分享一下学习的经验. 首先声明一下,这篇文章讲的是Redux的基本概念和实现,不包括react- ...

  8. Redux教程1:环境搭建,初写Redux

    如果将React比喻成士兵的话,你的程序还需要一位将军,去管理士兵(的状态),而Redux恰好是一位好将军,简单高效: 相比起React的学习曲线,Redux的稍微平坦一些:本系列教程,将以" ...

  9. Redux教程2:链接React

    通过前面的教程,我们有了简单的环境,并且可以运行Redux的程序,也对 如何编写Redux示例 有了初步的印象: 掌握了 使用Redux控制状态转移 ,继而驱动 React 组件发生改变,这才是学习R ...

随机推荐

  1. 实际中理解div布局和浮动

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. 需要插入子集的时候如何更新父级ID

    场景模拟: 我们需要在不同的新闻站点中采集新闻信息,  所以需要在数据库中保存一个新闻站点表(Site) 一个新闻表(News) 两表之间的关系是        Site(1)-News(N) 数据库 ...

  3. AJAX里调用AJAX,作定时进度刷新

    这个确实搞了一段时间,但成就感有啦... 哈哈,这个自动部署平吧,异步队列CELERY+REDIS,发布进度实时AJAX的技术点全部打通!!! 而获取实时进度,我用的是RESTFUL FRAMEWOR ...

  4. WINDOWS下,中文JSON格式读取报错处理:ValueError: No JSON object could be decoded

    File "C:\Python27\lib\json\__init__.py", line 290, in load **kw) File "C:\Python27\li ...

  5. 【HDU 3810】 Magina (01背包,优先队列优化,并查集)

    Magina Problem Description Magina, also known as Anti-Mage, is a very cool hero in DotA (Defense of ...

  6. ExecutorService生命周期

    ExecutorService接口继承了Executor接口,定义了一些生命周期的方法 public interface ExecutorService extends Executor { void ...

  7. AS 学习笔记 for in 和 for each in

    for in 的速度比 for each  in 慢很多倍 ~~ var myArray:Array = new Array(); myArray["a"] = "zer ...

  8. JSOI2015 Round1——完挂

    感觉眼前天地转了转…… Day 0 和zxy,zyh一同坐车去扬中,同行的还有llr 路上zyh基本在睡觉…… 入住的宾馆各种坑爹,同一层住的两个房间一个有网一个没网 我有幸入住了有网的房间,zyh在 ...

  9. TCP三次握手四次断开

    今天被问到三次握手了,当时只是脑子里有印象,却忘了一些SYN细节,手动微笑. 这么下去还怎么混...赶紧复习个... 三次握手是什么? TCP是面向连接的,无论哪一方向另一方发送数据之前,都必须先在双 ...

  10. maven install 跳过 测试 test

    你可能想要配置 Maven 使其完全跳过单元测试. 可能你有一个很大的系统,单元测试需要花好多分钟来完成,而你不想在生成最终输出前等单元测试完成. 你可能正工作在一个遗留系统上面,这个系统有一系列的失 ...