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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@zhujianshi/use-lens

v0.0.17

Published

a small react state manager with amazing lens

Readme

@zhujianshi/use-lens

创建可被react组件局部共享的值。使用lens进行值的存取。

Install

npm install @zhujianshi/use-lens

# or use yarn
yarn add @zhujianshi/use-lens

Quick Start

import { createShared } from '@zhujianshi/use-lens';
class A {
  a = 'string';
}
type ShareState={
  a:{
    b:{
      c: number;
    }
  },
  value: string;
  instance: A;
}



const { useLens } = createShared({
  instance: new A(),
  value:'hhh',
});


// in your component
useLens(['value']) // ['hhh',setV],
useLens(['A','a']) // ['string',setV],

API

createShared

const {
  useLens,
  useLensV,
  useSetLens,
  SharedProvider,
  storeLens,
} = createShared<DataType>(initailData);

useLens的类型为:

const [ focusedValue,setFocusedValue ] = useLens(pathOfValue); //pathOfValue为用于lensPath构建的数组

//or pass your custom lens
const [ focusedValue,setFocusedValue ] = useLens(yourLens);

useGetting的类型为:

const focusedValue = useGetting(pathOfValue);//pathOfValue也可以替换为yourLens;

正如它名字那样,useSetLens用来设置关注值的新值,值得注意的是它并不会在当前组件观测关注值

useSetting的类型为:

const setValue = useSetting(pathOfValue);//pathOfValue也可以替换为yourLens;

正如它名字那样,useSetting用来设置关注值的新值,值得注意的是它并不会在当前组件观测关注值

之前的版本中的useSetValue现在已经被useSetting所取代,并会在后续版本中移除。

SharedProvider为组件:

interface SharedProviderProps{
  initialValue?: PartialValue; 
  value?: Value;
}

SharedProvider内置了一个新的数据源(类型与全局数据源一样),并重定向了其后代组件的数据源指向,这一特性使得在编写在同一页面多次重复出现的复杂组件时较为方便。

之前版本的onChange已经被放弃,转而使用ref获得SharedProvider的实例,例子如下:

const ref = React.useRef(null);
<SharedProvider ref={ref}></SharedProvider>

const state = ref?.current?.getState();

Detail

setValue的行为差异变化

由于内置依赖(@zhujianshi/lens)[https://www.npmjs.com/package/@zhujianshi/lens]的变化,现在setValue(prev=>prev)并不会调度更新。

减少listeners的调用次数

在一次更新过程中,同步更新同一数据源的不同局部,可能会导致执行通知回调的过程耗时较长(在同一数据源上监听过多组件时现象比较明显)。此时,选择将数据源的同步更新和react的调度更新拆分开来(即多次同步更新数据源,并异步通知组件更新),可以有效降低无效监听次数。针对这种情况,提供了一下两个辅助函数:

declare const microBundled = (fn: ()=>void):void; // 将fn中产生的更新所引发的调度放在下一刻的微队列上(Promise.resolve(()=>{调度更新})
declare const macroBundled = (fn: ()=>void):void; // 将fn中产生的更新所引发的调度放在下一刻的宏队列上(setTimeout(()=>{调度更新})