Redux基本理念

Redux 是 JavaScript 状态容器,提供可预测化的状态管理

简单代码

import { createStore } from 'redux'

function counterReducer(state = { value: 0 }, action) {
  switch (action.type) {
    case 'counter/incremented':
      return { value: state.value + 1 }
    case 'counter/decremented':
      return { value: state.value - 1 }
    default:
      return state
  }
}

let store = createStore(counterReducer)

store.subscribe(() => console.log(store.getState()))

store.dispatch({ type: 'counter/incremented' })        // {value: 1}
store.dispatch({ type: 'counter/incremented' })        // {value: 2}
store.dispatch({ type: 'counter/decremented' })       // {value: 1}

设计思想

  • Web 应用是一个状态机,视图与状态是一一对应的。
  • 所有的状态,保存在一个对象里面。

适用场景

  • 用户的使用方式复杂
  • 不同身份的用户有不同的使用方式(比如普通用户和管理员)
  • 多个用户之间可以协作
  • 与服务器大量交互,或者使用了WebSocket View要从多个来源获取数据

概念和API

用户接触的是View层,State修改会导致View更新。
Store
Store 就是保存数据的地方。整个应用只能有一个 Store。用createStore生成

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

State
某个时点所有数据的快照,是个对象。用getState生成

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

Action
View发出的通知,运送数据到Store,表示State要修改了
Action 是一个对象。其中的type属性是必须的,表示 Action 的名称。其他属性可以自由设置

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

Action Creator

生成 Action的函数,因为每次有个Action就要写一遍会很麻烦

const ADD_TODO = '添加 TODO';

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

const action = addTodo('Learn Redux');

store.dispatch()

是 View 发出 Action 的唯一方法。store.dispatch接受一个 Action 对象作为参数,将它发送出去。

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

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;
  }
};


import { createStore } from 'redux';
const store = createStore(reducer);  //   store.dispatch方法会触发 Reducer 的自动执行

Reducer是纯函数,里面不能改变 State,必须返回一个全新的对象

// State 是一个对象
function reducer(state, action) {
  return Object.assign({}, state, { thingToChange });
  // 或者
  return { ...state, ...newState };
}

// State 是一个数组
function reducer(state, action) {
  return [...state, newItem];
}

store.subscribe()
Store 允许使用store.subscribe方法设置监听函数,一旦 State 发生变化,就自动执行这个函数。
把 View 的更新函数(对于 React 项目,就是组件的render方法或setState方法)放入listen,就会实现 View 的自动渲染。

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

store.subscribe(listener);

store.subscribe方法返回一个函数,调用这个函数就可以解除监听。

let unsubscribe = store.subscribe(() =>
  console.log(store.getState())
);

unsubscribe();

store的简单实现

const createStore = (reducer) => {
  let state;
  let listeners = [];

  const getState = () => state;

  const dispatch = (action) => {
    state = reducer(state, action);
    listeners.forEach(listener => listener());
  };

  const subscribe = (listener) => {
    listeners.push(listener);
    return () => {
      listeners = listeners.filter(l => l !== listener);
    }
  };

  dispatch({});

  return { getState, dispatch, subscribe };
};

工作原理

在这里插入图片描述

最简单的实现

const Counter = ({ value, onIncrement, onDecrement }) => (
  <div>
  <h1>{value}</h1>
  <button onClick={onIncrement}>+</button>
  <button onClick={onDecrement}>-</button>
  </div>
);

const reducer = (state = 0, action) => {
  switch (action.type) {
    case 'INCREMENT': return state + 1;
    case 'DECREMENT': return state - 1;
    default: return state;
  }
};

const store = createStore(reducer);

const render = () => {
  ReactDOM.render(
    <Counter
      value={store.getState()}
      onIncrement={() => store.dispatch({type: 'INCREMENT'})}
      onDecrement={() => store.dispatch({type: 'DECREMENT'})}
    />,
    document.getElementById('root')
  );
};

render();
store.subscribe(render);

参考:阮一峰:Redux入门教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Redux中的Counter是一个示例,用来展示如何使用Redux来管理应用程序的状态。Counter通常用来记录一个数字的状态,并提供增加和减少这个数字的操作。 在Redux中,Counter的状态被保存在一个称为Store的对象中。Store是Redux的核心概念之一,它保存了整个应用程序的状态树,并提供了一些方法来管理状态。 为了创建一个Counter,我们需要定义一个称为Reducer的函数。Reducer是一个纯函数,用于根据给定的操作类型来更新状态。在Counter中,Reducer可以处理两种操作类型:增加和减少。 我们还需要定义一些Action来表示用户的操作。在Counter中,可以有两个Action:INCREMENT和DECREMENT。 最后,我们需要创建一个Redux Store,并将Reducer传递给它。我们还可以使用Redux提供的一些辅助函数来处理状态的更新和访问。 以下是一个简单的Counter示例: ```javascript // Counter Reducer const counterReducer = (state = 0, action) => { switch (action.type) { case 'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; default: return state; } }; // Counter Actions const increment = () => { return { type: 'INCREMENT' }; }; const decrement = () => { return { type: 'DECREMENT' }; }; // Create Redux Store const store = Redux.createStore(counterReducer); // Dispatch Actions store.dispatch(increment()); store.dispatch(increment()); store.dispatch(decrement()); // Get Current State console.log(store.getState()); // Output: 1 ``` 在上面的示例中,我们定义了一个Counter Reducer来处理增加和减少操作。我们还定义了两个Action:INCREMENT和DECREMENT。然后,我们创建了一个Redux Store并将Reducer传递给它。最后,我们分发了一些Action来更新状态,并使用`store.getState()`来获取当前状态。 这只是Redux中Counter的基本概念,实际应用中可能会更复杂。但是这个示例可以帮助你理解Redux中如何管理应用程序的状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值