react-笔记

来源:互联网 发布:中国8月金融数据 编辑:程序博客网 时间:2024/06/05 20:19

combineReducers:

function visibilityFilter(){}
function todos(){}
import { combineReducers, createStore } from 'redux';
let reducer = combineReducers({ visibilityFilter, todos });
let store = createStore(reducer);

把几个reducer(visibilityFilter, todos)合成一个reducer。合并为一个switch/case

===========================================================

createStore:

Store 就是保存数据的地方,你可以把它看成一个容器。整个应用只能有一个 Store。
Redux 提供createStore这个函数,用来生成 Store。
import { createStore } from 'redux';
const store = createStore(fn);

------------------------------------------------

getState:

Store对象包含所有数据。如果想得到某个时点的数据,就要对 Store 生成快照。这种时点的数据集合,就叫做 State。
当前时刻的 State,可以通过store.getState()拿到。

import { createStore } from 'redux';
const store = createStore(fn);
const state = store.getState();

------------------------------------------------

Action :

State 的变化,会导致 View 的变化。但是,用户接触不到 State,只能接触到 View。所以,State 的变化必须是 View 导致的。Action 就是 View 发出的通知,表示 State 应该要发生变化了。
Action 是一个对象。其中的type属性是必须的,表示 Action 的名称。

const action = {
  type: 'ADD_TODO',
  payload: 'Learn Redux'
};

------------------------------------------------

dispatch:

store.dispatch()是 View 发出 Action 的唯一方法。

import { createStore } from 'redux';
const store = createStore(fn);

store.dispatch({
  type: 'ADD_TODO',
  payload: 'Learn Redux'
});
store.dispatch接受一个 Action 对象作为参数,将它发送出去。

------------------------------------------------

Store 收到 Action 以后,必须给出一个新的 State,这样 View 才会发生变化。这种 State 的计算过程就叫做 Reducer。
Reducer 是一个函数,它接受 Action 和当前 State 作为参数,返回一个新的 State。

const reducer = function (state, action) {
  // ...
  return new_state;
};

------------------------------------------------

subscribe:

Store 允许使用store.subscribe方法设置监听函数,一旦 State 发生变化,就自动执行这个函数。

------------------------------------------------

action:就是灯的变化,"红变绿"等,用名词表述 state:就是灯的名字,红灯、绿灯等,用名词表述 reducer:就是灯的变化规则,红灯之后是绿灯等,用状态转移表述,归根到底也是名词 store:就像是交警,执行上述的交通规则;

------------------------------------------------



============================





0 0
原创粉丝点击