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 🙏

© 2024 – Pkg Stats / Ryan Hefner

type-redux

v2.0.2

Published

[![npm version](https://badge.fury.io/js/type-redux.svg)](https://badge.fury.io/js/type-redux)

Downloads

17

Readme

强类型的Redux状态管理

npm version

  • 🌿 源于redux,借鉴vuex

  • 💪 使用ts完美约束了类型

  • 🚀 无redux样板代码

  • 🌏 享用redux生态

已经有了redux,为什么还会出现这个

让我们先来看看裸写redux大致需要哪些步骤:

在裸写redux时,通常需要维护以常量命名的action


const FOOACTION = 'FOOACTION'

然后再dispath


import FOOACTION from '...'

dispath({type: FOOACTION, payload: ''})

在reducer函数中以switch处理不同的action


import FOOACTION from '...'

function reducer(state, action) {
  switch(action.type) {
    case FOOACTION:
      return ...
  }
}
  • 以常量命名action可以使代码更加规范与约束,但也造成了大量的模板代码

  • 常量命名action只约束了action没有约束payload

  • 在reducer中以switch处理不同action写法太丑

type-redux 如何解决

  1. action被约束

action

借助ide的智能提示,能很清楚的看见所有action,省去了大量以常量命名action的模板代码。再也不用担心action写错。

  1. payload的约束

payload

不同的action,有着不同的payload。type-redux将其一一对应,能很清楚的知道action需要什么payload。

  1. 一个函数就是一个reducer,对应一个action

reducer

以一个个的函数组成一个个reducer,函数名就为action名称。这样比写一个超大的以switch处理action的reducer要优雅、清晰的多。

设计理念

起源于 redux 、借鉴 vuex 、以 typescript 进行约束。

type-redux

  • Store: 数据源,保存着state。

  • Muation 改变 State 的唯一途径。纯函数、无副作用,通过 commit 方法调用。

  • Action 发生副作用的地方,可通过调用 commit 发送 mutation 更新 State 。通过 dispatch 方法调用。

快速开始

  1. 安装包
npm install type-redux
  1. 声明state与类型定义
// ./index.ts
const initialState = {
  state1: 0,
  state2: '',
  state3: false
};

export type IState = typeof initialState;
export type IGetState = () => IState;
  1. mutation
// ./mutations.ts
export function add(getState: IGetState, num: number) {
  return { ...getState(), count: getState().count + num };
}
  1. action
// ./actions.ts
// ICtx 之后会定义
export async function fetchNewestCount(ctx: ICtx) {
  await ctx.dispatch('fetchRepurl');
  const result = await fetch(ctx.getState().repUrl).then((raw) => raw.json());
  ctx.commit('set', result.length);
  ctx.commit('loading', false);
}
  1. 创建Store
// ./index.ts
import { applyMiddleware, createStore } from 'type-redux';
import * as actions from './actions';
import * as mutations from './mutations';
...

const reducers = { mutations, actions };

export const store = createStore(initialState, reducers);

type IState = typeof initialState;
export type IGetState = () => IState;
export type ICtx = TypeRedux.IContext<IState, typeof reducers['mutations'], typeof reducers['actions']>;
  1. 监听store变更
store.subscribe(() => {
  console.log(store.getState())
})
  1. 使用
store.commit('add', 1);
store.dispatch('fetchNewestCount');
  1. 中间件的使用(可选)
import { createLogger } from 'redux-logger';
import { applyMiddleware, createStore } from 'type-redux';

...

export const store = createStore(initialState, reducers, applyMiddleware(createLogger()));

在React中使用

type-redux-hook

中间件列表

Example

简单例子

react例子

redux-observable例子

type-redux-rxjs例子