[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); // 0

store.dispatch( {type: ‘INCREMENT‘} );
console.log(store.getState()); // 1

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); // 0

store.dispatch( {type: ‘INCREMENT‘} );
console.log(store.getState()); // 1

const render = ()=> {
  document.body.innerText = store.getState();
}
render();
store.subscribe( render);

document.addEventListener(‘click‘, ()=>{
  store.dispatch( {type: ‘INCREMENT‘} );
})
时间: 2024-08-07 20:05:35

[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 GET_CATEGORIES = "GET_CATEGORIES"; /** * INIT VALUE */ export const initialCategories = [ {id: 0, name: 'Development'}, {id: 1, name: 'Design'},

Redux:store

Store是一个对象.他有如下职责: 1.存放state 2.对外提供访问state的接口: getState() 3.允许state更新:dispatch(action) 4.注册监听器: subscribe(listener) 5.注销监听器,通过subscribe返回的函数 redux所在的应用只能有一个store实例,如果想将state操作分解成多个逻辑,只能将Reducer的代码分解为多个部分,以函数调用的方式提取出来处理各自的逻辑. 当我们已经有一个处理state的Reducer函数

redux ,react-redux梳理,以及源码的学习

React Store:提供的方法{ store.dispatch() store.subscribe(() => { this.forceUpdate(); // this.setState({}); }); store.getState() } // import { createStore, applyMiddleware } from "redux"; import { createStore, applyMiddleware } from "../kredux

createStore第一个参数reducer

上一篇解读了 createStore 方法,重点以createStore 返回值 getState, dispatch 以及 subscribe  为知识点,接下来我们聊聊参数. 首先提一个问题:redux中包含可以包含几个reducer?答案是:1个.因为参数只有一个接收reducer,如果你有多个reducer,最后也要通过combineReducers合并为一个 .reducer简单来说是一个方法,接受初始值state和指示值action两个参数: function testReducer

[Redux] Implementing Store from Scratch

Learn how to build a reasonable approximation of the Redux Store in 20 lines. No magic! const counter = (state = 0, action) => { switch (action.type) { case 'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; default: return state; } }

Redux在处理庞大Store并频繁进行更新操作时的性能

Q:当你拥有一个相当大的 SPA 拥有许多状态,因为有很多页面,模块,子模块和许多元素.所有子状态都关联 App 中不同的关注点,子状态由它们自己的 reducer 处理.但是当进行非常频繁的更新操作,所有的 reducer 都将被调用. 当拥有一个如下的 store: store: { subStore1: { subSubstore1: {} ... subSubstore10: {} }, subStore2: { subSubstore1: {} ... subSubstore10: {

redux 介绍及配合 react开发

前言 本文是 Redux 及 Redux 配合 React 开发的教程,主要翻译自 Leveling Up with React: Redux,并参考了 Redux 的文档及一些博文,相对译文原文内容有增减修改. 目录 前言 目录 什么是 Redux,为什么使用 Redux Redux 的三大基本原则 1.唯一数据源 2.State 是只读的 3.使用纯函数来执行修改 第一个 Redux Store 不要改变原 State , 复制它 复合 Reducer Dispatch 之后哪个 Reduc

Redux 的基础概念-API

三个基本原则 整个应用只有唯一一个可信数据源,也就是只有一个 Store State 只能通过触发 Action 来更改 State 的更改必须写成纯函数,也就是每次更改总是返回一个新的 State,在 Redux 里这种函数称为 Reducer Actions Action 很简单,就是一个单纯的包含 { type, payload } 的对象,type 是一个常量用来标示动作类型,payload 是这个动作携带的数据.Action 需要通过 store.dispatch() 方法来发送. 比

redux+flux(一:入门篇)

React是facebook推出的js框架,React 本身只涉及UI层,如果搭建大型应用,必须搭配一个前端框架.也就是说,你至少要学两样东西,才能基本满足需要:React + 前端框架. Facebook官方使用的是 Flux 框架.本文就介绍如何在 React 的基础上,使用 Flux 组织代码和安排内部逻辑. 首先,Flux将一个应用分成四个部分: Flux 的最大特点,就是数据的"单向流动". 用户访问 View View 发出用户的 Action Dispatcher 收到