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

@wxa/redux

v2.5.1

Published

redux for wxa

Downloads

121

Readme

@wxa/redux

NPM version npm bundle size (minified + gzip)

redux小程序适配方案,在小程序开发中使用redux管理全局状态。

安装

# 使用npm安装
npm i -S @wxa/redux

用例

  1. app.js/app.wxa中挂载插件。
// app.js or app.wxa
import {App, wxa} from '@wxa/core';
import {wxaRedux, combineReducers} from '@wxa/redux'
import promiseMiddleware from 'redux-promise';

wxa.use(wxaRedux, {
    reducers: combineReducers(...your reducer),
    middlewares: [promiseMiddleware]
})

@App
export default class Main {};

挂载成功后,插件会在App、Component、Page实例中挂载store到$store。通过$store.getState()可以获得所有全局状态。

  1. 在页面/组件类中定义mapState对象,指定关联的全局状态(在react中叫connect)。
import {Page} from '@wxa/core';

@Page
export default class Index {
    mapState = {
        todolist : (state)=>state.todo,
        userInfo : (state)=>state.userInfo
    }

    add() {
        // dispatch change state.
        // todo list will auto add one.
        this.$store.dispatch({type: 'Add_todo_list', payload: 'coding today'});
    }
}

然后再template中就可以直接使用映射的数据了。

<view>{{userInfo.name}}</view>
<view wx:for="{{todolist}}">{{key+1}}{{item}}</view>

得益于@wxa/core的diff方法,redux在同步数据的时候只会增量的修改数据,而不是全量覆盖。:grin:

技术细节

wxa/redux根据不同的实例类型有不同的任务,在App层,我们需要创建一个store并挂载到app中,在PageComponent层,我们做了更多细节处理。

  • App Level
    创建store,应用redux的中间件,挂载store到App实例。

  • Page Level
    在不同的生命周期函数,有不同的处理。

    • onLoad 根据mapState订阅store的数据,同时挂载一个unsubscribe方法到实例。
    • onShow 标记页面实例$$isCurrentPagetrue, 同时做一次状态同步。因为有可能状态在其他页面做了改变。
    • onHide 重置$$isCurrentPage,这样子页面数据就不会自动刷新了。
    • onUnload 调用$unsubscribe取消订阅状态
  1. Component Level
    针对组件生命周期做一些单独处理
    • created 挂载store
    • attached 订阅状态,并同步状态到组件。
    • detached 取消订阅