Skip to content

Redux TechStack

Published: at 16:00

前端工程化 · 第 51 篇,共 56 篇

import { createStore } from "redux";

// 在 React 项目中需要维护: action/counter.js
const INCREMENT = "INCREMENT";
const DECREMENT = "DECREMENT";

// 在 React 项目中需要维护: reducer/counter.js
function counter(state = 0, action) {
  switch (action.type) {
    case INCREMENT:
      return state + 1;
    case DECREMENT:
      return state - 1;
    default:
      return state;
  }
}

// 在 React 项目中需要维护: store.js
const store = createStore(counter);
store.subscribe(() => console.log(store.getState()));

// 在 React 项目中需要维护: containers/Counter.js
store.dispatch({ type: INCREMENT });
// 1
store.dispatch({ type: INCREMENT });
// 2

Redux TechStack