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

reducer-container

v1.0.18

Published

解决redux代码常量冗余问题,提供更新状态接口规范

Downloads

37

Readme

Reducer

"use strict";
const ReducerContainer =require("../index")

// 前提是已经使用了redux的connect方法将 component 和 reducer 绑定,
class Reducer extends ReducerContainer {
    store = {
        isClick: false
    }
    // 直接调用父类的update函数更新页面
    toggleStatus(clicked) {
        this.update('isClick', clicked);
    }

    // 返回一个immutable对象可以自动更新
    autoUpdate(click) {
        return this.getState().set("isClick", click);
    }

    // 返回promise  在resolve或者 reject 后更新状态,前提是传入的参数必须是immtuable.Map 对象
    updateWithPromise(click) {
        return new Promise((resolve, reject) => {
            setTimeout(
                () => resolve(this.getState().set("isClick", click)), 5000);
        })
    }

    // 如果返回的不是一个immutable 对象,那么不会触发更新
    simpleReturn() {
        console.log(1)
        return "simple";
    }

    // 有前缀的函数不会被拦截
    _normal() {
    }
}
export default new Reducer();

Componet


import React from "react";
import reducer from "./reducer";
export default class Component extends React.Component{
    render(){
        // 在任何地方调用该方法会返回当前component对应的store,避免数据层层透传所带来的麻烦
        // 方便在跨组件中共享数据
        let store = reducer.getState();
        let isClick =store.get("isClick");
        return (
            <div>
            {isClick?"clicked":"click me"}
            {/* 直接调用 update 更新状态 */}
            <button onClick={()=>reducer.toggleStatus(!isClick)}>call update</button>
             {/* 返回一个新的状态后,reducerContainer会拦截返回值,如果该返回值是Immutable.Map那么就更新状态,否则什么也不做 */}
        <button onClick={()=>reducer.autoUpdate(!isClick)}>return new state</button>
         {/* 异步更新,你的action 返回一个promise对象,知道该promise resolve 或者reject的时候,会更新状态,常常用于异步加载数据,该操作方式默认支持防重复点击,上一个promise 没有被解决,那么永远不会有任何反应 */}
        <button onClick={()=>reducer.updateWithPromise(!isClick)}>(async) wait for 5s</button>
        </div>
    )
    }
}
```