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

@dyljhd/use-effect-debugger

v1.2.1

Published

A type-safe React hook for debugging purposes that wraps around the useEffect hook, which returns the dependancies that changed on each iteration of the effect within the console.

Downloads

873

Readme

Important!

Do NOT use in production environment!

This package is intended to be a debugging tool only! Therefore, it should be installed within the devDependancies and all usage removed from the codebase before pushing to a production environment.

Reference

Parameter Explanation

  • effect: Accepts a function that contains imperative, possibly effectful code.
  • deps: The effect will only activate if the values in the list change.
  • debugOptions: A selection of options to customize debug output within the console.
    • consoleOutput: This changes the console output method for the changed deps in the console.
    • consoleName: This changes the debug label outputted with the changed deps in the console.
    • depNames: This gives each of the changed deps in the object a named key instead of defaulting to its index in the deps array.

Parameter Types

  • effect: React.EffectCallback
  • deps: React.DependencyList
  • debugOptions
    • consoleOutput: "log" | "table" | undefined
    • consoleName: string | undefined
    • depNames: (string | null)[] | undefined

Extra Details

  • effect and deps are no different from useEffect arguments.
  • consoleName defaults to use-effect-debugger.
  • You can pass null within the depNames array if you would like to skip naming a particular key.
  • On mount, a dep's prev value will always be undefined.
  • A consoleOutput of log outputs using console.log, and table outputs using console.table

Example Usage

Code:

function ExampleComponent() {
  const [string, setString] = useState('0');
  const [number, setNumber] = useState(0);

  useEffectDebugger(
    () => {
      console.log('useEffect ran');
    },
    [string, number],
    {
      consoleName: 'USE-EFFECT-DEBUGGER',
      depNames: [null, 'Number'],
    }
  );

  function incrementString() {
    setString((prev) => String(Number(prev) + 1));
  }

  function incrementNumber() {
    setNumber((prev) => prev + 1);
  }

  function incrementAll() {
    incrementString();
    incrementNumber();
  }

  return (
    <>
      <p>String: {string}</p>
      <p>Number: {number}</p>
      <button onClick={incrementString}>Increment String</button>
      <button onClick={incrementNumber}>Increment Number</button>
      <button onClick={incrementAll}>Increment All</button>
    </>
  );
}

Console Output:

On mount:

"USE-EFFECT-DEBUGGER" {
  0: {
    prev: undefined,
    cur: "0"
  },
  Number: {
    prev: undefined,
    cur: 0
  },
}

"Increment String" onClick event:

"USE-EFFECT-DEBUGGER" {
  0: {
    prev: "0",
    cur: "1"
  }
}

"Increment Number" onClick event:

"USE-EFFECT-DEBUGGER" {
  Number: {
    prev: 0,
    cur: 1
  }
}

"Increment All" onClick event:

"USE-EFFECT-DEBUGGER" {
  0: {
    prev: "1",
    cur: "2"
  },
  Number: {
    prev: 1,
    cur: 2
  },
}