Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
use-deep-compare
Advanced tools
React hooks, except using deep comparison on the inputs, not reference equality
The `use-deep-compare` package provides React hooks that perform deep comparison of dependencies to determine if they have changed, which can be useful for optimizing performance and avoiding unnecessary re-renders.
useDeepCompareEffect
The `useDeepCompareEffect` hook works similarly to `useEffect`, but it performs a deep comparison of the dependencies. This is useful when you have complex objects or arrays as dependencies and want to avoid unnecessary re-renders.
import { useDeepCompareEffect } from 'use-deep-compare';
import React, { useState } from 'react';
const MyComponent = () => {
const [data, setData] = useState({ key: 'value' });
useDeepCompareEffect(() => {
console.log('Effect triggered due to deep comparison');
}, [data]);
return <div>{data.key}</div>;
};
useDeepCompareMemo
The `useDeepCompareMemo` hook works similarly to `useMemo`, but it performs a deep comparison of the dependencies. This is useful for memoizing values derived from complex objects or arrays.
import { useDeepCompareMemo } from 'use-deep-compare';
import React, { useState } from 'react';
const MyComponent = () => {
const [data, setData] = useState({ key: 'value' });
const memoizedValue = useDeepCompareMemo(() => {
return data.key;
}, [data]);
return <div>{memoizedValue}</div>;
};
The `react-use` package is a collection of essential React hooks, including hooks for deep comparison like `useDeepCompareEffect` and `useDeepCompareMemo`. It offers a broader range of hooks for various use cases, making it a more comprehensive solution compared to `use-deep-compare`.
The `ahooks` package is a React Hooks library that provides a wide range of hooks, including deep comparison hooks like `useDeepCompareEffect` and `useDeepCompareMemo`. It is well-documented and widely used, offering more features and utilities beyond deep comparison.
Use Deep Compare - React hooks, except using deep comparison on the inputs, not reference equality. Read about Hooks feature.
npm i use-deep-compare
yarn add use-deep-compare
pnpm add use-deep-compare
bun add use-deep-compare
import React from "react";
import { useDeepCompareEffect } from "use-deep-compare";
function App({ object, array }) {
useDeepCompareEffect(() => {
// do something significant here
return () => {
// return to clean up that significant thing
};
}, [object, array]);
return <div>{/* render significant thing */}</div>;
}
import React from "react";
import { useDeepCompareCallback } from "use-deep-compare";
function App({ object, array }) {
const callback = useDeepCompareCallback(() => {
// do something significant here
}, [object, array]);
return <div>{/* render significant thing */}</div>;
}
import React from "react";
import { useDeepCompareLayoutEffect } from "use-deep-compare";
function App({ object, array }) {
useDeepCompareLayoutEffect(() => {
// perform layout effects
}, [object, array]);
return <div>{/* render component */}</div>;
}
import React from "react";
import { useDeepCompareMemo } from "use-deep-compare";
function App({ object, array }) {
const memoized = useDeepCompareMemo(() => {
// do something significant here
}, [object, array]);
return <div>{/* render significant thing */}</div>;
}
import React from "react";
import { useDeepCompareImperativeHandle } from "use-deep-compare";
function App({ object, array }) {
const ref = React.useRef();
useDeepCompareImperativeHandle(ref, () => ({
// create imperative methods
}), [object, array]);
return <div ref={ref}>{/* render component */}</div>;
}
To receive eslint warnings about missing array dependencies, just like for standard useEffect
, useCallback
, useMemo
, ... hooks - extend you eslint config as follows:
{
"rules": {
// ...
"react-hooks/exhaustive-deps": ["warn", {
"additionalHooks": "(useDeepCompareEffect|useDeepCompareCallback|useDeepCompareMemo|useDeepCompareImperativeHandle|useDeepCompareLayoutEffect)"
}]
}
}
FAQs
React hooks, except using deep comparison on the inputs, not reference equality
The npm package use-deep-compare receives a total of 127,573 weekly downloads. As such, use-deep-compare popularity was classified as popular.
We found that use-deep-compare demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.