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

@dpdfe/mobx-signal

v1.0.1

Published

本库有两个独立的功能:

Downloads

2

Readme

mobx-signal

本库有两个独立的功能:

自动包裹 observer()

为您的组件自动包裹 observer()

在入口文件中引入:

import { enableAutoWrapObserver } from "@dpdfe/mobx-signal";

enableAutoWrapObserver({
  // 自动包裹策略
  // 如果要自动全包裹,则直接return true
  // 如果指定某些情况才包裹,可以根据传入的组件进行包裹。
  // 如第三方库antd等都不要包裹,仅我们自己的组件包裹。
  // 可配合webpack插件等,将你src目录中的组件的displayName自动添加$$$
  // 如:若函数组件的函数名以$$$结尾才包裹:
  // return Component.name.endsWith('$$$') || Component.displayName.endsWith('$$$')
  addObserverIf(Component) {
    return true;
  }
});

signal

注意,不需要上述方法中的自动包裹 observer(),您也可以使用 signal。

有两种方法:使用 useSignal 创建,或使用 signal() 配合 useLocalObservable 在 hooks 组件中创建;

useSignal

直接使用 {count},不要使用 {count.value},以达到最高性能。

import { signal, useSignal } from "@dpdfe/mobx-signal";

export default function App() {
  const count = useSignal(0);
  console.log("Parent render");

  return (
    <>
      <button onClick={count.value++}>increment</button>
      useSignal: {count}
    </>
  );
}

signal()

可以看到在点击按钮时,父子组件均不会重新渲染(父子里面的console.log在点击按钮时,均不会打印),仅 {store.username} 部分会重新渲染。

import { signal, useSignal } from "@dpdfe/mobx-signal";

function Parent() {
  const store = useLocalObservable(() => ({
    username: signal("zhangsan"),
    changeUsername() {
      store.username.value = "username" + new Date().getTime();
    },
  }));

  console.log("Parent render");

  return (
    <>
      <button onClick={store.changeUsername}>changeUsername</button>
      <SignalChild store={store} />
    </>
  );
}


function SignalChild({ store }: any) {
  console.log("SignalChild render");
  return <div>SignalChild Child: {store.username}</div>;
}

export default Parent;