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

@helux/hooks-impl

v4.4.2

Published

helux hooks implement lib

Downloads

913

Readme

helux-hooks-impl

useObject

使用普通对象,需注意此接口只接受普通对象,应用里使用 useObject 替代 React.useState 将享受到以下两个好处

  • 方便定义多个状态值时,少写很多 useState
  • 内部做了 unmount 判断,让异步函数也可以安全的调用 setState,避免 react 出现警告 : "Called SetState() on an Unmounted Component" Errors
const [obj, setObj] = useObject({ num: 1, age: 2 });
setObj({ num: 2 }); // 只需要传修改的值即可,内部会自动合并

useMutable

深层次的修改需要使用 useMutable 替代 useObject

const [state, setState] = useMutable({ info: { age: 1 } });
setState((draft) => (draft.info.age = 2));

useEffect

对齐 React.useEffect,但优化了调用逻辑,即 strict 模式与普通模式行为一致,只有一次 mount 与 unmount 产生

import { useEffect } from 'helux';

function Comp() {
  useEffect(() => {
    log('helux.useEffect', 'print one time at strict mode');
    return () => log('helux.useEffect', 'cleanup');
  }, []);
  React.useEffect(() => {
    logRed('React.useEffect', 'print 2 times at strict mode');
    return () => logRed('React.useEffect', 'cleanup');
  }, []);
  return <MarkUpdate>test useEffect</MarkUpdate>;
}

useStable

生成稳定的对象,对象的所有方法将转为稳定引用,且回调里始终可以读到外部的最新值,无闭包陷阱

function Comp(props: any) {
  const [obj, setObj] = useObject({ num: 1 });
  // srv 是稳定的,srv.readState,srv.readProps,srv.changeState 是稳定的
  const srv = useStable({
    readState() {
      console.log(`%c read state num ${obj.num}`, `color:green`);
    },
    readProps() {
      console.log(`%c read props num ${props.num}`, `color:green`);
    },
    changeState() {
      setObj({ num: random() });
    },
  });

  // 传入值,则只是返回最新值
  const numTwo = useStable(2);

  // 如传入单函数,则返回的稳定的函数引用
  const fn = useStable(() => {
    console.log(`%c read state num ${obj.num}`, `color:green`);
  });
}