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

grey-react-box

v1.0.8

Published

https://gitee.com/grey-ts/grey-react-box

Downloads

160

Readme

轻量级的 分模块方式的数据状态管理

github

https://gitee.com/grey-ts/grey-react-box

  • 初始化

// box 的 状态
// 我们可以任意的添加我们需要的字段
export class UserInfoState {
    num = 0; // 这里是我们的一个例子
}


// box 对象
// 继承 GreyReactBox泛型为box状态对象
export default class UserInfo extends GreyReactBox<UserInfoState> {
    constructor() {
        super(new UserInfoState()); // 初始化box状态数据
    }

    // 通过 pipeline 方法来创建add方法,add执行完后,会触发关联了box状态的组件的渲染
    // boxStart 当前box的状态
    // option 调用add方法时的参数(必须为可选参数)
    add = this.pipeline(async(boxStart, option?:number) => {
        boxStart.num += option || 0;
    })
}
  • 通过hook关联数据 (推荐)
import { useContext } from 'grey-react-box';
import UserInfo from './UserInfo';

// 模块跟初始化
export default () => {

    // 初始化 box 实例
    const controller = React.useMemo(() => new UserInfo(), []);

    return (
        // 设置 box 的跟节点
        <Source box={controller}>
            <Main />
        </Source>
    )
}

const Main = () => {
    
    // 通过 useContext 获取到根部box的实例
    const controller = useContext<UserInfo>();

    // 通过 box的 useState 方法关联box的 num 属性
    const num = controller.useMemo(e => e.num, []);

    // 点击按钮事件,点击后给box的num属性加1 
    const onClickUserInfo = React.useCallback(() => {
        controller.pipeline(async boxStart => {
            boxStart.num += 1;
        })()
    })
    
    return (
        <div>
             {/* 按钮调用 onClickUserInfo 方法,并显示 num */}
            <Button onClick={onClickUserInfo} >+1:{num}</Button>

            {/* 按钮调用 controller.add 方法,并显示 num */}
            <Button onClick={() => { controller.add(2) }} >+2:{num}</Button>
        </div>
    )
})
  • 通过装饰器绑定数据
import { decorator, Source, ISourceChildProps } from 'grey-react-box';
import UserInfo from './UserInfo';

// 模块跟初始化
export default () => {

    // 初始化 box 实例
    const controller = React.useMemo(() => new UserInfo(), []);

    return (
        // 设置 box 的跟节点
        <Source box={controller}>
            <Main />
        </Source>
    )
}

const Main = decorator<any, ISourceChildProps<UserInfo>>(
    Source.connect() // 把根部的box实例注入到props里
)(props => {
    
    // 获取到box实例
    const { controller } = props;

    // 通过 box的 useState 方法关联box的 num 属性
    const num = controller.useMemo(e => e.num, []);

    // 点击按钮事件,点击后给box的num属性加1 
    const onClickUserInfo = React.useCallback(() => {
        controller.pipeline(async boxStart => {
            boxStart.num += 1;
        })()
    })
    
    return (
        <div>
            {/* 按钮调用 onClickUserInfo 方法,并显示 num */}
            <Button onClick={onClickUserInfo} >+1:{num}</Button>

            {/* 按钮调用 controller.add 方法,并显示 num */}
            <Button onClick={() => { controller.add(2) }} >+2:{num}</Button>
        </div>
    )
})