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

dva-toolkit

v0.1.0

Published

A great dva typescript support toolkit inspired by @redux/toolkit

Downloads

541

Readme

Dva Toolkit

Dva Toolkit 是为了给 Dva 社区带来 @redux/toolkit 一样的体验而诞生的强类型支持工具,提供自然且良好的类型推断体验,可以在大多数情况下省去类型声明,除了 Payload 部分必须使用 PayloadAction 声明。

const { model, action: { add, addDelay }, initState } = createDvaSlice({
  namespace: 'count',
  state: 0,
  reducers: {
    add (state, { payload }: PayloadAction<number|void>) {
      return state + (payload || 1)
    }
  },
  effects: {
    * addDelay ({ payload }: PayloadAction<number>, { put, call }) {
      yield call(delay, 100)
      yield put({ type: 'add', payload })
    }
  }
})

app.model(model)
app.start()
app._store.dispatch(addDelay(2))
expect(app._store.getState().count).toEqual(0)
setTimeout(() => {
  expect(app._store.getState().count).toEqual(2)
  done()
}, 200)

Install

npm i dva-toolkit

yarn add dva-toolkit

核心函数

本项目目前只提供 createDvaSlice 一个函数,提供类似 @redux/toolkitcreateSlice 类似的功能。

createDvaSlice 的参数就和 dva 的 model 配置一摸一样,这是为了完美兼容过去的项目,因此过度起来十分简单,对过去的代码没有任何侵入性。createDvaSlice 是个支持范型的函数,因此如果希望指定 state 的类型可以直接 createDvaSlice<IState>({...}),但该函数返回的 initState 可以用作类型推断,所以没有必须声明状态类型的情况。

const { initState } = createDvaSlice({...})
type IState = typeof initState

目前 createDvaSlice 函数的返回对象只有 modelactioninitState 属性。model 是直接注入 dva 中的对象,action 包含所有生成 action 的函数。 initState 属性则是只用来类型推断,永远不会变化的初始状态。

如上面的代码展示的那样,action 包含两个自动生成的函数 add, addDelay

const add = (payload: number|void) => ({payload, type:'count/add'})

const addDelay = (payload: number) => ({payload, type:'count/addDelay'})

注意,必须要将 reducer 和 effect 里的 action 定义成 PayloadAction 类型,函数的参数才能正确推断,否则无法使用该项目。PayloadAction 是遵守 redux 约定,将信息都放在 payload 里的类型。

action 中的函数是用来生成 {payload: ..., type} 对象的,你还需要把生成的对象放到 dispatch 中

dispatch(add(1))

开发者的话

2021 年,大家广泛的拥抱 typescript,但是 dva 的维护力度并不强势,如果你想要选择一个状态管理工具,redux + @redux/toolkit、mobx 都是很好的选择。这个项目旨在为 umi 使用者和一些使用 dva 的历史项目提供更好的开发体验,不作为 dva 的宣传力量。