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

cyanx

v1.1.1

Published

Cyanx基于ReactHook的,针对函数组件的简单、可扩展的状态管理器

Downloads

22

Readme

CyanX

CyanX是一款基于ReactHook的,针对函数式组件的极简、可扩展的状态管理器
设计哲学——任何状态,无论在哪都应该简单快速地获得

特性

  • 极简,1分钟上手,两行代码
  • 无限层级叠加,告别组件之间复杂的传值
  • 极低的内存消耗,数据按需加载,组件所需的状态值变化时,才会重新渲染
  • 基于ReactHook,仅支持React函数式组件开发

兼容环境

现代浏览器和 IE11

安装

  • npm

$ npm install cyanx

  • yarn

$ yarn add cyanx

核心概念

Public Store(公用仓库)

存放多个组件均需使用的状态的仓库

存放的数据类型

  • 值类型(基本类型):字符串(String)、数字(Number)、布尔(Boolean)、对空(Null)、未定义(Undefined)、Symbol
  • 引用数据类型:对象(Object)、数组(Array)、函数(Function)
  • 函数方法

withCyanxObserver(观察者

观察指定的公用仓库&使用此公用仓库中状态的可观察组件,当公用仓库的某个状态值发生变化时,会重新渲染使用了此状态的可观察组件

withCyanxObserver(component, publicStoreName, publicStoreDefaultValues)是一个高阶组件HOC,有3个参数

| 参数名 | 是否必填 | 说明 | | ---- | ---- | ---- | | component | *必填 | 需转化为观察者的组件 | | publicStoreName | *必填 | 需观察公用仓库的名称 | | publicStoreDefaultValues | *必填 | 公用仓库的默认值 |

withCyanxObservable(可观察的)

将组件转为可观察状态,并得到所需的公用仓库的状态(存于props中),当使用的公用仓库的状态值改变时,将自动重新渲染,并得到最新的公用仓库的状态

withCyanxObservable(component, publicStoreName, stateKeyArray)是一个高阶组件HOC,有3个参数

| 参数名 | 是否必填 | 说明 | | ---- | ---- | ---- | | component | *必填 | 需转化为可观察的组件 | | publicStoreName | *必填 | 需引入的公用仓库的名称 | | stateKeyArray | 选填 | 组件需引用的公用仓库中状态的Key名称,引入的state会直接存入可观察组件的props中 |

dispatch(调遣)

dispatch是一个函数function,用于改变公用仓库的状态

特征

  • 每一个公用仓库都会有唯一一个dispatch函数

  • dispatch会存在可观察组件的props中,函数名为${公用仓库的名称}Dispatch;或可观察组件的props中的名为${公用仓库的名称}的对象。此中有dispatch的元素 例子:有一个公用仓库publicStore可观察组件C观察了公用仓库publicStore可观察组件C获取公用仓库publicStoredispatch可以通过一下两种方式拿到: props.publicStoreDispatch || props.publicStore.dispatch

  • dispatch改变状态的规则,同React中的setState()函数

实例

常量

// 公用仓库的名称
const PublicStoreName = 'publicStore';// 公用仓库的名称,自己定义

// 公用仓库默认值
const publicStoreDefaultValues = {
    a: 1,
    b: {},
    c: ()=>{},
}

设置观察者组件&需观察的公用仓库

import { withCyanxObserver } from 'cyanx';// 引入观察者HOC

// 观察者组件
const ComponentObserver = () => {
	...
	
	return (
	    <>
		    <ComponentObservable />
		</>
	)
}

export default withCyanxObserver(ComponentObserver, PublicStoreName, publicStoreDefaultValues);

转化可观察组件&组件所需的公用仓库的状态

import { withCyanxObservable } from 'cyanx';// 引入可观察的HOC

// 观察者组件
const ComponentObservable = ({a, b, c, publicStoreDispatch, ...props}) => {...}

||

const ComponentObservable = (props) => {
    const {a, b, c, dispatch} = props.publicStore;
}


export default withCyanxObservable(ComponentObservable, PublicStoreName, ['a', 'b', 'c']);

注意事项

出于内存的考虑,建议观察者组件的下1级的子组件均转化成可观察组件

GitHub

https://github.com/Bholder/CyanX

License

MIT

Copyright (c) 2020-present, Bholder CY