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

mpstore-next

v1.0.0

Published

一个轻量级的状态管理库,类似于`pinia`、`vuex`,适用于微信小程序。

Readme

一个轻量级的状态管理库,类似于piniavuex,适用于微信小程序。轻量使用,无需复杂的操作。

安装

npm install mpstore-next

使用

1、创建一个userStore文件,并初始化一个store对象:

import { defineStore } from 'mpstore-next'

export const useUserStore = defineStore("user",{
  state:() => ({
    userInfo:{
      name: "王五"
    },
    userToken:"",
  }),
  getters:{
    userName(state){
      return "UserName:" + state.userInfo.name
    }
  },
  actions:{
    changeName(name){
      this.userInfo.name = name
    }
  }
})

2、使用内置封装在Component中使用Store

import { defineComponent } from 'mpstore-next'
import { useUserStore } from 'userStore'

const userStore = useUserStore()

defineComponent({
  stores: { store: userStore },
  data: {},
  methods: {
    changeName(){
      userStore.changeName("张三")
    }
  }
})

或者原生方式在Component中使用Store

import { useUserStore } from 'userStore'

const userStore = useUserStore()
let closeStore = null

Component({
  data: {},
  lifetimes: {
    attached: function () {
      closeStore = userStore.$update((data) => this.setData(data))
    },
    detached: function () {
      closeStore && closeStore()
    }
  },
  methods: {
    changeName(){
      userStore.changeName("张三")
    }
  }
})

同样,Page中使用同上Component

import { definePage } from 'mpstore-next'
import { useUserStore } from 'userStore'

const userStore = useUserStore()

definePage({
  stores: { store: userStore },
  data: {},
  changeName(){
    userStore.changeName("张三")
  }
})

3、在wxml中使用store数据

<view>
  <text>Getter数据:{{ $getter.userName }}</text>
  <text>State数据: {{ $state.userToken }}</text>
  <button bindtap="changeName">更新store数据</button>
</view>

4、更新store数据

import { defineComponent } from 'mpstore-next'
import { useUserStore } from 'userStore'

const userStore = useUserStore()

defineComponent({
  stores: { store: userStore },
  data: {},
  methods: {
    changeName(){
      //直接更新store数据
      userStore.userInfo.name = "张三"
      userStore.userToken = "123456"
    },
    changeWithPatch(){
      //使用patch更新store数据
      userStore.$patch({
        userInfo:{
          name:"张三"
        },
        userToken:"123456"
      })
    }
  }
})

5、多个Store使用,区分Store命名空间,使用storePart: true开启store隔离

注:以defineStore中的key作为隔离名称

import { defineStore } from 'mpstore-next'

export const useThemeStore = defineStore("theme",{
  state:() => ({
    theme: "light",
  }),
})

使用多个store

import { defineComponent } from 'mpstore-next'
import { useUserStore } from 'userStore'
import { useThemeStore } from 'themeStore'

const userStore = useUserStore()
const themeStore = useThemeStore()

defineComponent({
  stores: {
    storePart: true, //开启store隔离
    store: [ userStore, themeStore ]
  },
  data: {},
})

在wxml中使用不同命名空间的store数据

<view>
  <text>用户名:{{ user.$state.userInfo.name }}</text>
  <text>主题: {{ theme.$state.theme }}</text>
</view>

6、按需使用属性

import { defineComponent } from 'mpstore-next'
import { useUserStore } from 'userStore'

const userStore = useUserStore()

defineComponent({
  stores: { 
    store: userStore,
    useProp: ['userName']
  },
  data: {},
  methods: {
    changeName(){
      userStore.changeName("张三")
    }
  }
})

多个store,按需使用属性

import { defineComponent } from 'mpstore-next'
import { useUserStore } from 'userStore'
import { useThemeStore } from 'themeStore'

const userStore = useUserStore()
const themeStore = useThemeStore()

defineComponent({
  stores: {
    usePart: true, //开启store隔离
    config: [
      {
        store: userStore,
        useProp: ['userName']		
      },
      {
        store: themeStore,
        useProp: ['theme']
      }
    ]
  },
  data: {},
  methods: {
    changeName(){
      userStore.changeName("张三")
    }
  }
})

8、store数据监听,支持state和getter的变化监听

import { defineComponent } from 'mpstore-next'
import { useUserStore } from 'userStore'

const userStore = useUserStore()
let stopWatch = null

defineComponent({
  stores: { userStore },
  data: {},
  lifetimes: {
    attached: function () {
      stopWatch = userStore.$watch(() => userStore.name, (newValue,oldValue) => {
        console.log("watch newValue = ", newValue, "oldValue = ", oldValue)
      })
    },
    detached: function () {
      stopWatch && stopWatch()
    }
  },
  methods: {
    changeName(){
      userStore.changeName("张三")
      // 点击之后订阅会打印出数据
      // watch newValue = 张三 oldValue = 王五
    }
  }
})

8、订阅数据变化

import { defineComponent } from 'mpstore-next'
import { useUserStore } from 'userStore'

const userStore = useUserStore()
let closeSubscribe = null

defineComponent({
  stores: { userStore },
  data: {},
  lifetimes: {
    attached: function () {
      closeSubscribe = userStore.$subscribe((data) => {
        console.log("subscribe",data)
      })
    },
    detached: function () {
      closeSubscribe && closeSubscribe()
    }
  },
  methods: {
    changeName(){
      userStore.changeName("张三")
      // 点击之后订阅会打印出数据
      // subscribe { "userInfo.name": "张三" }
    }
  }
})

注:由于微信小程序对Proxy的兼容性,目前仅支持微信小程序版本号2.11.0及以上的版本。