首页 > 其他 > 详细

【Redux初始-01】

时间:2021-06-03 14:09:10      阅读:23      评论:0      收藏:0      [点我收藏+]

一、、Redux适用场景:

  适用场景:多交互、多数据源

技术分享图片

 

 

 阮一峰Blog:http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_basic_usages.html

二、设计思想

Redux 的设计思想很简单,就两句话。

(1)Web 应用是一个状态机,视图与状态是一一对应的。

(2)所有的状态,保存在一个对象里面。

请务必记住这两句话,下面就是详细解释。

三、基本概念和 API

3.1 Store

Store 就是保存数据的地方,你可以把它看成一个容器。整个应用只能有一个 Store。

Redux 提供createStore这个函数,用来生成 Store。


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

上面代码中,createStore函数接受另一个函数作为参数,返回新生成的 Store 对象。

3.2 State

Store对象包含所有数据。如果想得到某个时点的数据,就要对 Store 生成快照。这种时点的数据集合,就叫做 State。

当前时刻的 State,可以通过store.getState()拿到。


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

const state = store.getState();  // 时点(时刻)的数据集合

Redux 规定, 一个 State 对应一个 View。只要 State 相同,View 就相同。你知道 State,就知道 View 是什么样,反之亦然。

3.3 Action

State 的变化,会导致 View 的变化。但是,用户接触不到 State,只能接触到 View。所以,State 的变化必须是 View 导致的。Action 就是 View 发出的通知,表示 State 应该要发生变化了。

Action 是一个对象。其中的type属性是必须的,表示 Action 的名称。其他属性可以自由设置,社区有一个规范可以参考。


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

上面代码中,Action 的名称是ADD_TODO,它携带的信息是字符串Learn Redux

可以这样理解,Action 描述当前发生的事情。改变 State 的唯一办法,就是使用 Action。它会运送数据到 Store。

3.4 Action Creator

View 要发送多少种消息,就会有多少种 Action。如果都手写,会很麻烦。可以定义一个函数来生成 Action,这个函数就叫 Action Creator。


const ADD_TODO = ‘添加 TODO‘;

function addTodo(text) {
  return {
    type: ADD_TODO,
    text
  }
}

const action = addTodo(‘Learn Redux‘);

上面代码中,addTodo函数就是一个 Action Creator。

3.5 store.dispatch()

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


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

store.dispatch({
  type: ‘ADD_TODO‘,
  payload: ‘Learn Redux‘
});

上面代码中,store.dispatch接受一个 Action 对象作为参数,将它发送出去。

结合 Action Creator,这段代码可以改写如下。


store.dispatch(addTodo(‘Learn Redux‘));

 

3.6 Reducer

Store 收到 Action 以后,必须给出一个新的 State,这样 View 才会发生变化。这种 State 的计算过程就叫做 Reducer。

Reducer 是一个函数,它接受 Action 和当前 State 作为参数,返回一个新的 State。


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

整个应用的初始状态,可以作为 State 的默认值。下面是一个实际的例子。


const defaultState = 0; const reducer = (state = defaultState, action) => { switch (action.type) { case ‘ADD‘: return state + action.payload; default: return state; } }; const state = reducer(1, { type: ‘ADD‘, payload: 2 });

上面代码中,reducer函数收到名为ADD的 Action 以后,就返回一个新的 State,作为加法的计算结果。其他运算的逻辑(比如减法),也可以根据 Action 的不同来实现。

 

为什么这个函数叫做 Reducer 呢?因为它可以作为数组的reduce方法的参数。请看下面的例子,一系列 Action 对象按照顺序作为一个数组。


const actions = [
  { type: ‘ADD‘, payload: 0 },
  { type: ‘ADD‘, payload: 1 },
  { type: ‘ADD‘, payload: 2 }
];

const total = actions.reduce(reducer, 0); // 3

上面代码中,数组actions表示依次有三个 Action,分别是加0、加1和加2。数组的reduce方法接受 Reducer 函数作为参数,就可以直接得到最终的状态3

【Redux初始-01】

原文:https://www.cnblogs.com/hqq422/p/14844521.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!