npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

my-sample-redux

v0.0.5

Published

自己实现一个简易的Redux

Readme

简易版 Redux 实现(学习版)

一个为学习 Redux 原理而手动实现的简易版 Redux。
包括 createStoreapplyMiddleware、中间件(Saga)等核心机制,适合学习和调试。

解析请关注我的CSDN博客 https://blog.csdn.net/weixin_40710412/article/details/147550588?spm=1001.2014.3001.5501


📖 目录


背景介绍

为了更好地理解 Redux 的内部原理,特地动手从零实现了一个简版 Redux。
项目整体保持 Redux 核心思想,同时精简了边界处理,便于学习和掌握。


快速使用

引入并使用自己的 createStoreapplyMiddlewaresagaMiddleware,即可完成状态管理和副作用处理。

  1. 初始化 store
  2. 绑定 DOM 事件
  3. 通过 dispatch 修改状态
  4. 订阅 state 更新,刷新界面

源码示例

import { ADD_NUM, FETCHDATA, SET_HUGE_DATA, SUB_NUM } from "./constants";
import defSaga from "./defSaga";
import { createStore } from "./lib";
import applyMiddleWare from "./lib/applyMiddleware";
import { Action } from "./lib/typings";
import createSagaMiddleware from "./middlewares/saga";

type ReducerState = {
  counter: number;
  hugeData: string;
};

interface ActionType extends Action {
  payload: any;
}

// Reducer 函数
function reducer(state: ReducerState, action: ActionType): ReducerState {
  switch (action.type) {
    case ADD_NUM:
      return { ...state, counter: state.counter + action.payload };
    case SUB_NUM:
      return { ...state, counter: state.counter - action.payload };
    case SET_HUGE_DATA:
      return { ...state, hugeData: action.payload };
    default:
      return (
        state || {
          counter: 0,
          hugeData: "",
        }
      );
  }
}

// 创建 Saga 中间件
const sagaMiddleware = createSagaMiddleware();

// 创建 Store,应用中间件
const store = createStore<ReducerState, ActionType>(
  reducer,
  applyMiddleWare(sagaMiddleware)
);

// 启动 saga
sagaMiddleware.run(defSaga);

// 绑定 DOM 元素
const plus10 = document.getElementById("plus10");
const minus10 = document.getElementById("minus10");
const mockDataShow = document.getElementById("mockData");
const fetchdataBtn = document.getElementById("fetchdata");
const counterShow = document.getElementById("counterShow");

// 绑定点击事件
plus10.addEventListener("click", () => {
  store.dispatch({ type: ADD_NUM, payload: 20 });
});

minus10.addEventListener("click", () => {
  store.dispatch({ type: SUB_NUM, payload: 20 });
});

fetchdataBtn.addEventListener("click", () => {
  store.dispatch({ type: FETCHDATA, payload: 20 });
});

// 更新页面显示
function updateDom() {
  counterShow.innerText = store.getState().counter + "";
  mockDataShow.innerText = store.getState().hugeData + "";
}

// 监听 state 变化
store.subscribe(() => {
  updateDom();
});

// 初始化页面
updateDom();