[Redux] Store Methods: getState(), dispatch(), and 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()); // 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()的更多相关文章
- [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 ...
- Redux:store
Store是一个对象.他有如下职责: 1.存放state 2.对外提供访问state的接口: getState() 3.允许state更新:dispatch(action) 4.注册监听器: subs ...
- [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 ...
- [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 ...
- [React Testing] The Redux Store - Multiple Actions
When using Redux, we can test that our application state changes are working by testing that dispatc ...
- Redux API之Store
Store Store 就是用来维持应用所有的 state 树 的一个对象. 改变 store 内 state 的惟一途径是对它 dispatch 一个action. Store 不是类.它只是有几个 ...
- 揭开redux,react-redux的神秘面纱
16年开始使用react-redux,迄今也已两年多.这时候再来阅读和读懂redux/react-redux源码,虽已没有当初的新鲜感,但依然觉得略有收获.把要点简单写下来,一方面供感兴趣的读者参考, ...
- redux详解
redux介绍 学习文档:英文文档,中文文档,Github redux是什么 redux是一个独立专门用于做状态管理的JS库(不是react插件库),它可以用在react, angular, vue等 ...
- Redux API
Redux API Redux的API非常少.Redux定义了一系列的约定(contract),同时提供少量辅助函数来把这些约定整合到一起. Redux只关心如何管理state.在实际的项目中 ...
随机推荐
- App Store审核指南(中文版)2010版
前言 感谢您付出宝贵的才华与时间来开发iOS应用程程序.从职业与报酬的角度而言,这对于成千上万的开发员来说一直都是一项值得投入的事业.我们希望帮助您加入这个成功的组织.这是我们首次发布<应用程序 ...
- jquery mobile展开项collapsible
代码演示 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...
- extjs之TypeError: d.read is not a function解决方案
在创建如下代码时报出此错:TypeError: d.read is not a function Ext.define('shebyxgl_sheb_model', { extend: 'Ext.da ...
- [jstl] forEach标签使用
在JSP的开发中,迭代是经常要使用到的操作.例如,逐行的显示查询的结果等.在早期的JSP中,通常使用Scriptlets来实现Iterator或者Enumeration对象的迭代输出.现在,通过JS ...
- 手写 title 提示
jquery实现 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...
- 转:ElasticSearch 插件安装
原文来自于:http://www.07net01.com/linux/Elasticsearch_chajiananzhuang_21257_1350993328.html 进入elasticsear ...
- 将excel里面的数据导入到程序里面
页面布局 <table> <tr> <td style="padding-top: 16px; padding-left: 36px;"> &l ...
- [EXCEL] 在单元格中自动输入时间和日期
选中需输入的单元格,直接按下“Ctrl+:”组合键可输入当前日期:如果直接按下“Ctrl+Shift+:”组合键即可输入当前时间:当然也可以在单元格中先输入其他文字然后再按以上组合键,如先输入“当前时 ...
- 使用Windows 系统性能监控来报警磁盘空间不足
http://blog.csdn.net/jiangxinyu/article/details/4370288
- BZOJ 1027 [JSOI2007]合金
1027: [JSOI2007]合金 Time Limit: 4 Sec Memory Limit: 162 MBSubmit: 2605 Solved: 692[Submit][Status][ ...