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

cf-mp-store

v1.0.4

Published

a miniProgram store

Readme

Cf-Mp-Store

一个响应式的小程序全局状态管理库

如何使用

1.安装依赖

npm install cf-mp-store

yarn add cf-mp-store

pnpm add cf-mp-store

2.创建store对象

import MpStore from 'cf-mp-store'

const userStore = new MpStore({
  state: {
    name: 'userStore'
  },
  actions: {
    changeNameAction({state, actions}, newName: string) {
      // this为store对象
      state.name = newName
    }
  }
})

export default userStore

3.使用store

// 小程序页面的ts文件
import userStore from 'xxx'
Page({
  data: {

  },

  onLoad() {
    userStore.useData(this, { 
      useKeys: ['name'],  // 所需引入的state的key
      // total: true,     // 是否需要引入整个store.state,默认为false,且优先级比useKeys高
      // immediate: true  // 是否立即执行 默认为true,后续介绍
      // cb: (key, value)  // 回调函数,默认为undefined,后续介绍
    })
  },
  onUnLoad() {
    userStore.unUseData(this)
  }
})
  • 只需要在页面的onLoad或者组件的attached调用store对象的useData即可将对应的数据保存到页面的data中。
  • 然后直接在wxml中使用即可。
  • 当组件或者页面销毁时,可以调用store.unUseData清除store与页面的联系

useData的参数说明

interface OptionsType {
  useKeys?: string[];
  cb?: (key: string, value: any) => any;
  immediate?: boolean;
  total?: boolean
}
// 第一个参数instance是小程序的页面或者组件实例
store.useData(instance, options: OptionsType)
  • useKeys用于指定所要引入的 store.state 。
  • total如果为true,则表示要将整个store.state都引入进来。
  • immediate:
    • 默认情况下,只要调用了store.useData(),那么就会立即调用this.setData()来将store的值保存到页面data中。如果immediate设置为false,那么调用store.useData()时不会立即调用this.setData(),而是等到下一次store.state发生变化时,才会调用this.setData()将值保存到页面data中。
    • 很多时候我们都会先在state中定义空数据,然后再调用dispatch去发送网络请求并且将请求结果保存到之前定义的空数据中,那么这个时候我们就可以将immediate设置为false。
import MpStore from 'cf-mp-store'

const userStore = new MpStore({
  state: {
    list: []
  },
  actions: {
    async fetchList({ actions, state }, ...args) {
      // 发送网络请求
      const res = await getList()
      state.list = res.list
    }
  }
})
// 小程序ts文件
import userStore from 'xxx'
Page({
  data: {},
  onLoad: {
    userStore.useData(this, {
      useKeys: ['list'],
      immediate: false
    })
    // immediate为false,所以这里的时候,data中并没有list数据
    // 调用dispatch去发送网络请求,然后再将结果保存到store中。修改了state.list值,会执行this.setData()
    userStore.dispatch('fetchList')
  }
})
  • cb参数(推荐使用箭头函数):
    • 如果传递了cb参数,那么在state发生变化时,不会调用this.setData(),而是会回调cb。
    • 当我们需要对引入的state的key进行重命名时,可以通过传递cb参数实现。
import MpStore from 'cf-mp-store'

const userStore = new MpStore({
  state: {
    name: 'userStore'
  }
})

// 小程序ts文件
Page({
  data: {},
  onLoad: {
    userStore.useData(this, {
      useKeys: ['name'],
      cb: (key, value) => {
        const newKey = `${key}123`  // name123
        this.setData({
          [newKey]: value
        })
      }
    })
  }
})