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

minipro-store

v1.1.7

Published

一个简单的微信小程序store仓库

Downloads

11

Readme

使用方法

基于小程序的特性,简单实现了一个小程序的 store 仓库

  • 1.响应式更新页面
  • 2.使用简单
  • 3.代码量少

1.安装

在小程序的根目录下载

 npm i minipro-store 或 yarn add minipro-store

2 引入以及使用

注意事项:如果出现小程序tabbar页面切换后dispatch无法刷新页面,请在tabbar页面中的onShow执行app.store.connect(this)方法

小程序使用 npm 包,需要通过 npm 构建,自行百度,构建完成后 App.js 创建一个仓库

import CreateStore from 'minipro-store';
import state from './store/index';
// state 仓库数据, tabBar: []  //tabBar页面的路径:如:pages/index/index,无tabBar页面可不传
const store = new CreateStore({ state, tabBar: [] });
App({
  onLaunch() {},
  store,
});

store/index.js

// 这里就是我们的数据全局保存的地方
export default {
  userInfo: {},
  name: '',
};

pages/index/index.js

const app = getApp();
Page({
  onLoad() {
    app.store.connect(this);
  },
  onTap() {
    app.store.dispatch({
      name: '我是页面',
    });
  },
});

components/index/index.js

const app = getApp();
Component({
  lifetimes: {
    attached: function () {
      app.store.connect(this);
    },
  },
  methods: {
    onTap() {
      app.store.dispatch({
        name: '我是组件',
      });
    },
  },
});

useEffect

很多时候,我们需要监听某个属性是否改变,如,登录成功后,判断 isLogin 是否为 true,然后再发送请求数据 可在页面或组件中写入 useEffect,该函数返回一个数组,第一个参数是一个函数,第二个参数是一个 keys 数组 如下监听 name 的变化,变化后会触发函数的执行,页面加载完成后会执行一遍

onLoad() {
  app.store.connect(this)
},
useEffect() {
  return [(next,prev) => {
    console.og('prev',prev)  //上一个值
    console.og('next',next)  //修改后的值
  },['name']
}

页面和组件的用法一致,都是在页面或组件创建的使用,通过 connect 方法订阅消息 然后通过 dispatch 修改 store 中的数据,并更新页面或组件的数据,diaptch 接受的参数必须是一个对象, 通过 store.state.name 直接修改 state 的数据并不会刷新页面 然后在对应的组件或者页面,可通过 store 变量访问到仓库中的数据 index.wxml

<view>
{{store.name}}
</view>

更新日志

5-12

本次更新,添加了监听某个属性是否更新的功能

// pages/index/index
Page({
  onLoad() {
    app.store.connect(this)
  },
  useEffect(
    return [(next,prev) => {
    console.og('prev',prev)  //上一个值
    console.og('next',next)  //修改后的值
  },['name'])]
})

修改了 app.store.effect 的用法

app.store.effect({
  count: count + 1
})
//或
getValue() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        count: 999
      })
    },1000)
  })
},
onClick() {
  app.store.effect(this.getValue())
}

4-25

本次更新,解决了切换 tabbar 页面后(A -> B, B ->A),在 tabbar 页面注册的组件无法刷新的问题 new CreateStore 方法进行了改变

new CreateStore(store)

->
new CreateStore({
  state,
  //这是页面的tabber页面的路径,如果定义了tabbar页面,必传,否则会出现tabbar页面的组件数据无法更新的问题
  tabBar: ['pages/index/index']
})

4-22

本次更新,引入了 effect 方法 使用:

// 参数1,可接受一个action或者一个函数,参数2,修改store数据成功后的回调
async getValue(name) {
  const res = await new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        name: '我是name'
      })
    },1000)
  })
  return res
},
onTap() {
  app.store.effect(() => this.getValue(),(state) => {
      console.log('23',state)
  })
}