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

track-dependancy-changes

v1.0.2

Published

A lightweight React utility that tracks the changes in component dependencies before and after each render. It also shows the number of times a component has re-rendered, and allows aliasing of dependency names for easier readability.

Downloads

250

Readme

Track-Dependency-Changes

A lightweight React utility that tracks the changes in component dependencies before and after each render. It also shows the number of times a component has re-rendered, and allows aliasing of dependency names for easier readability.

Features

  • Track which dependencies in a component have changed before and after each render.
  • Display the number of times a component has re-rendered.
  • Alias dependency names for easier tracking and readability.
  • Print a detailed table showing:
    • Old Value: The previous value of the dependency.
    • New Value: The current value of the dependency.
    • Change: Whether the dependency has changed (✔️ or ).

Blog

For a more detailed guide, check out this blog post.

Installation

Install the package via npm:

npm install track-dependancy-changes

Usage

Here’s how you can use the useTrackDependency hook in a React component to monitor changes in its dependencies:

Example

tsx

import React, { useState, useEffect, useCallback } from "react";
import {useTrackDependency} from "track-dependancy-changes"; // Import the hook

const ExampleComponent: React.FC = () => {
  // States to track
  const [count, setCount] = useState(0);
  const [text, setText] = useState("Hello");
  const [complexState, setComplexState] = useState({ name: "Alice", age: 25 });

  // A callback function (tracked)
  const handleClick = useCallback(() => {
    setCount((prevCount) => prevCount + 1);
  }, []);

  // Use the hook to track dependencies and render changes
  useTrackDependency(
    [count, text, complexState, handleClick], // Dependencies to track
    ["Count", "Text", "Complex State", "Handle Click"], // Labels for easier readability
    "ExampleComponent" // Component name (optional)
  );

  return (
    <div>
      <h1>useTrackDependency Example</h1>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Increase Count</button>

      <p>Text: {text}</p>
      <button onClick={() => setText(text === "Hello" ? "World" : "Hello")}>
        Toggle Text
      </button>

      <p>Complex State: {JSON.stringify(complexState)}</p>
      <button
        onClick={() =>
          setComplexState((prevState) => ({
            ...prevState,
            age: prevState.age + 1,
          }))
        }
      >
        Increase Age
      </button>
    </div>
  );
};

export default ExampleComponent;

Explanation of the Hook

The useTrackDependency hook allows you to monitor state or prop changes in your component, as well as log useful debugging information to the console during each render.

Parameters:

  1. dependencies (required): An array of the dependencies (state, props, or functions) you want to track.
  2. labels (optional): An array of strings to alias your dependencies in the logs, making it easier to identify them.
  3. componentName (optional): A string representing the name of the component, to make console logs more identifiable.

Console Output:

Whenever your component renders, the hook logs a table to the console showing:

  • Old Value: The value of the dependency before the render.
  • New Value: The value of the dependency after the render.
  • Change: A green tick (✔️) if the value has changed, or a red cross () if it has not.

Each render will also be logged with the total number of renders of the component.

Example Console Log:

ExampleComponent Rendering 2 times
┌─────────┬────────────────────┬──────────┬───────────┬─────────┐
│ (index) │    Dependency      │ Old Value│ New Value │ Change  │
├─────────┼────────────────────┼──────────┼───────────┼─────────┤
│    0    │       Count        │    0     │    1      │    ✔️   │
│    1    │       Text         │ "Hello"  │ "Hello"   │    ❌   │
│    2    │   Complex State    │ {name:…} │ {name:…}  │    ❌   │
│    3    │    Handle Click    │   func   │   func    │    ❌   │ 
└─────────┴────────────────────┴──────────┴───────────┴─────────┘

Benefits:

  • Debugging: It helps identify which dependencies are causing re-renders and tracks the number of renders for better performance monitoring.
  • Readability: By allowing dependency aliasing, the logs become much clearer, especially in complex components.
  • Flexible: You can track any combination of state, props, or functions and see exactly when and why the component re-renders.

License

This package is licensed under the MIT License. See the LICENSE file for details.

Author