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

@nodecg/react-hooks

v1.0.3

Published

Custom React Hooks for NodeCG

Downloads

30

Readme

@nodecg/react-hooks

This package is a collection custom hooks of React Hooks for NodeCG API.

🚨 This package is in alpha state. But feel free to try out and file an issue for suggestion/bugs!

Install

npm install @nodecg/react-hooks
# or
yarn add @nodecg/react-hooks
# or
pnpm add @nodecg/react-hooks

About React Hooks

(This section comes from when React hooks was just introduced as an alpha feature, but is still useful to keep as an introduction.)

The React Hooks are a new way of sharing code between components, introduced in version 16.8.

Please read the documentation of React Hooks thoroughly before using them.

It also helps to learn the background mechanism of React Hooks. React hooks: not magic, just arrays

Recommendation

Use eslint-plugin-react-hooks in your project. It is 100% smarter than you to detect violation of the Rules of Hooks.

Usage

useReplicant

  • Subscribes to specified replicant and returns the value as state.
  • Allows you to use replicant values in function component.
import { useReplicant } from "@nodecg/react-hooks";

// This component will re-render when the `counter replicant value changes
export function RunnerName() {
  const [count, setCount] = useReplicant("counter");
  return (
    <div>
      <div>{count}</div>
      <button onClick={() => setCount(count + 1)} />
    </div>
  );
}

useListenFor

  • Subscribes messages with listenFor, and unlistens on unmount.
  • Combining with other hooks enables powerful stateful features with function component
import { useListenFor } from "@nodecg/react-hooks";

// Shows modal for 1 second when NodeCG receives 'errorHappened' message from the server
export function AlertOnMessage() {
  const [showAlert, setShowAlert] = useState(false);
  useListenFor("errorHappened", () => {
    setShowAlert(true);
  });
  useEffect(() => {
    if (!showAlert) {
      return;
    }
    // Disappear alert 1 second after
    const timer = setTimeout(() => {
      setShowAlert(false);
    }, 1000);
    // Make sure to return cleanup function
    return () => {
      clearTimeout(timer);
    };
  }, [showAlert]);

  return <Modal show={showAlert} />;
}

License

MIT © Keiichiro Amemiya (Hoishin)