console.clear();
const counter = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
} // Create a store
const {createStore} = Redux;
// Store hold the state, can accept a reducer function
const store = createStore(counter); let currentState = store.getState();
console.log(currentState); // store.dispatch( {type: 'INCREMENT'} );
console.log(store.getState()); // store.subscribe( () => {
document.body.innerText = store.getState();
}); document.addEventListener('click', ()=>{
store.dispatch( {type: 'INCREMENT'} );
})

If we run we will miss the first init state, it starts from '2'...

So we need to call it before subscribe:

console.clear();
const counter = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
} // Create a store
const {createStore} = Redux;
// Store hold the state, can accept a reducer function
const store = createStore(counter); let currentState = store.getState();
console.log(currentState); // store.dispatch( {type: 'INCREMENT'} );
console.log(store.getState()); // const render = ()=> {
document.body.innerText = store.getState();
}
render();
store.subscribe( render); document.addEventListener('click', ()=>{
store.dispatch( {type: 'INCREMENT'} );
})

[Redux] Store Methods: getState(), dispatch(), and subscribe()的更多相关文章

  1. [AngularJS] Write a simple Redux store in AngularJS app

    The first things we need to do is create a reducer: /** * CONSTANT * @type {string} */ export const ...

  2. Redux:store

    Store是一个对象.他有如下职责: 1.存放state 2.对外提供访问state的接口: getState() 3.允许state更新:dispatch(action) 4.注册监听器: subs ...

  3. [Functional Programming ADT] Create a Redux Store for Use with a State ADT Based Reducer

    With a well defined demarcation point between Redux and our State ADT based model, hooking up to a R ...

  4. [Redux-Observable && Unit Testing] Use tests to verify updates to the Redux store (rxjs scheduler)

    In certain situations, you care more about the final state of the redux store than you do about the ...

  5. [React Testing] The Redux Store - Multiple Actions

    When using Redux, we can test that our application state changes are working by testing that dispatc ...

  6. Redux API之Store

    Store Store 就是用来维持应用所有的 state 树 的一个对象. 改变 store 内 state 的惟一途径是对它 dispatch 一个action. Store 不是类.它只是有几个 ...

  7. 揭开redux,react-redux的神秘面纱

    16年开始使用react-redux,迄今也已两年多.这时候再来阅读和读懂redux/react-redux源码,虽已没有当初的新鲜感,但依然觉得略有收获.把要点简单写下来,一方面供感兴趣的读者参考, ...

  8. redux详解

    redux介绍 学习文档:英文文档,中文文档,Github redux是什么 redux是一个独立专门用于做状态管理的JS库(不是react插件库),它可以用在react, angular, vue等 ...

  9. Redux API

    Redux API ​ Redux的API非常少.Redux定义了一系列的约定(contract),同时提供少量辅助函数来把这些约定整合到一起. ​ Redux只关心如何管理state.在实际的项目中 ...

随机推荐

  1. ViewPager 嵌套Listview 让Listview响应 ViewPager 左右滑事件

    一段拦截判断而已.   之前一直误解了一个拦截的描述.导致搞了半天. 结论: onInterceptTouchEvent 返回true,就由本身View的onTouchEvent进行事件消费. /** ...

  2. Swift - 08 - 元组

    //: Playground - noun: a place where people can play import UIKit // 元组就是将多个不同的值集合成一个数据 /* 元组是Object ...

  3. Windows 7如何限制运行特定的应用程序(转载)

    AppLocker即"应用程序控制策略",是Windows 7系统中新增加的一项安全功能. 步骤/方法 1 单击"开始"菜单,单击"控制面板" ...

  4. KeyPress事件

    在做一个小demo的时候,发现在文本框中输入一个数字,按下“+”,数字增加了,但是“+”仍旧存在的问题,解决方案:提前执行键盘press事件 private void txtNum_KeyPress( ...

  5. 大数据学习之hadoop伪分布式集群安装(一)公众号undefined110

    hadoop的基本概念: Hadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储. Hadoo ...

  6. Forms & HTML 组件 - laravelcollective/html

    简书链接 :Forms & HTML 组件 - laravelcollective/html 安装 方法一: composer require laravelcollective/html 方 ...

  7. Egret 入门

    居然使用 TyptScript... 先贴手册地址:http://www.typescriptlang.org/docs/tutorial.html. 先要接受一个诡异的写法: private loa ...

  8. 温故而知新 C++ 类型转换

    C语言类型转换 在C语言里用到的类型转换方式,一般都是用强制类型转换,语法:(类型说明符)(表达式),例如: (float)a 把a转换为实型,(int)(x+y) 把x+y的结果转换为整型.C语言这 ...

  9. Spring Assert.notNull

    Exception in thread "main" java.lang.IllegalArgumentException: Source must not be null at ...

  10. 【HDOJ】3500 Fling

    题意巨难懂.简言之,就是球互相碰撞时,主动碰撞的球将会停止,另一个球将沿着碰撞方向继续移动,不断碰撞.但是无法弹射紧挨着的球,但是若a弹射b,bc相邻,这种情况b可以弹射c. #include < ...