New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

react-use-compare-debugger

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-use-compare-debugger

Allows comparing two state/prop trees for differences

latest
Source
npmnpm
Version
1.0.8
Version published
Maintainers
1
Created
Source

react-use-compare-debugger

This React hook enables you to compare two different sets of props/state between re-renders. It's designed to aid with debugging to help determine why React components are re-rendering by offering a redux style console output.

image

It logs the name of the property, followed by both the previous and current values. Finally it renders a flag indicating whether the two values are referentially equal which is important to prevent un-necessary React re-renders. It will try to warn you about any values that are strictly equal but referrentially different as this suggests a lack of memoization.

Installation

npm install --save-dev react-use-compare-debugger

Usage

To use simply call the useCompareDebugger hook from a component where you want to log changes. For example:

import useCompareDebugger from "react-use-compare-debugger";

const myHelloWorldComponent = (props) => {
    useCompareDebugger("myHelloWorldComponent", props, ["deepNestedObject"])

    return (<div>Hello World</div>);
}

You can also use it with hooks:

import useCompareDebugger from "react-use-compare-debugger";

const myHelloWorldComponent = () => {
    const result = myCustomHook();
    useCompareDebugger("myHelloWorldComponent", { result });

    return (<div>Hello World</div>);
}

Parameters

ParameterTypeRequiredDescription
componentStringShould be the name of the component. This is used to group your logs together and help you know the source.
valueObjectThis is the object from this render, that you'd like to compare against the previous render.
ignoreKeysArrayAn array of key names, used to ignore diving into deep/complex nested objects.

Notes

There is a hard limit of 100 items deep with an array/object hierarchy to prevent accidental infinite recursion of self-referrential objects.

TODO: Add tests

Thanks

Thanks to LogRocket - this was inspired by their blog post https://blog.logrocket.com/how-to-get-previous-props-state-with-react-hooks/ and a need for some debugging.

Keywords

react

FAQs

Package last updated on 27 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts