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. 微信返回上一页,页面中的AJAX的请求,对Get请求无效的解决办法

    问题产生原因       最近在做一个微信的项目时,遇到一种很常见的情况,需求是这样的,当用户进入到"我的个人中心"的时候,会有一个点击跳转填写认证资料的按钮,点击此按钮后,会跳转 ...

  2. Java中异常的基本应用(一)

    在Java中,我们把异常当做一种对象来处理,正是异常机制的引入,使得我们的程序更加健壮.异常指示了一个不正常的条件,或者一个错误条件,简单地说就是一个中断了正常的指令流的事件.程序控制将无条件的抛至一 ...

  3. 跟我学android-Android应用结构分析(四)

    自动生成的R.java文件说明 public final class R { public static final class attr { } public static final class ...

  4. 【USACO 2.1.1】城堡

    [题目描述] 我们憨厚的USACO主人公农夫约翰(Farmer John)以无法想象的运气,在他生日那天收到了一份特别的礼物:一张“幸运爱尔兰”(一种彩票).结果这张彩票让他获得了这次比赛唯一的奖品— ...

  5. TalkingData游戏版本在Cocos2d-x 3.2使用

    最近一直忙别的方面的事情,没有太关注cocos2dx的发展情况,竟然已经更新到了3.2的版本,总的来说3.2比较3.0在使用上会有一些路径的变成,包括ios的引用路径和android上的build的p ...

  6. 关于一个隐藏和显示物品列表的demo

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  7. ThinkPHP 使用极光推送给ios推送消息

    HTML <div id="wrap"><a href="<{:U('Push/pushData')}>">推送</a ...

  8. java中加载xml文件方法

    this.getclass().getclassloader().getresourceasstream(String file); 可以加载文件,比如xml.

  9. iOS开发——C篇&数组与指针

    2015-07-17 13:23 编辑 前面我们介绍了关于C语言的内存分配问题,下面我们就开始介绍关于C语言的两个非常重要的知识点:数组与指针 数组与指针其实不仅仅是再C语言中,再OC中(当然OC是内 ...

  10. shell之小括号、中括号、大括号

    1.Shell中变量的原形:${var}  一串命令的执行 #等价于 $ var=test $ echo $var test #例如,用在这个位置 $ echo ${var}AA testAA 2.命 ...