Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-what-changed

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-what-changed

React What Changed - track which dependency changed between cycles

  • 1.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

React What Changed

An efficient and simple library to show which dependency changed in a hook between react cycles.

Installation

npm install react-what-changed --save-dev

API

  • reactWhatChanged
  • reactWhatDiff
  • whatDiff

reactWhatChanged

Description

invoke this function again and again on each react's cycle to print which dependency has changed

Import

import { reactWhatChanged } from 'react-what-changed';

Examples

Let's say we have the following component

import { reactWhatChanged as RWC } from 'react-what-changed';
function MyComponent(props) {
    const [somePrimitive, setSomePrimitive] = useState(123);
    const someArray = useSomeArray();
    const someObject = useMemo(() => {
        return { person: { name: 'John', age: 99 } };
    }, [someArray]);
    ...
    ...
    return <div></div>;
}

Example #1: simplest log as array

useEffect(() => {
    someLogic();
}, RWC([somePrimitive, someArray, someObject]));

Example #2: verbose log as array

useEffect(() => {
    someLogic();
}, RWC([somePrimitive, someArray, someObject], true));

Example #3: use ID for several logs

useEffect(() => {
    someLogic();
}, RWC([somePrimitive], false, 'id_1'));

useMemo(() => {
    someLogic();
}, RWC([someArray], false, 'id_2'));

useCallback(() => {
    someLogic();
}, RWC([someObject], false, 'id_3'));

Example #4: log with dependencies names

// Instead of:
useEffect(() => {
    someLogic();
}, RWC([
    somePrimitive,
    someArray,
    someObject.person.name,
    someObject.person.age,
]);

// Do this:
useEffect(() => {
    someLogic();
}, RWC({
    primitive: somePrimitive,
    arr: someArray,
    name: someObject.person.name,
    age: someObject.person.age,
});

reactWhatDiff

Description

invoke this function again and again on an object (or array) to print all changes (deep comparison)

Import

import { reactWhatDiff } from 'react-what-changed';

Examples

Let's use the same component from reactWhatChanged example.

Example #1: simple log

import { reactWhatDiff as RWD } from 'react-what-changed';
useEffect(() => {
    someLogic();
}, [somePrimitive, someArray, RWD(someObject)]);

Example #2: use ID for several logs

useEffect(() => {
    someLogic();
}, [somePrimitive, RWD(someArray, 'id_1'), RWD(someObject, 'id_2')]);

whatDiff

Description

print all changes (deep comparison) between 2 objects (or arrays)

Import

import { whatDiff } from 'react-what-changed';

Examples

Example #1: log the diffs between 2 objects

import { whatDiff as WD } from 'react-what-changed';
const obj1 = { name: 'John', address: { city: 'New York' } };
const obj2 = { name: 'John', address: { city: 'Paris' } };
WD(obj1, obj2);

Let's use the same component from reactWhatChanged example.

Example #2: simple log in a react component

const originalObject = useRef(someObject);
someLogic();
WD(someObject, originalObject.current);

License

React What Changed is APACHE-2.0 licensed.

Keywords

FAQs

Package last updated on 03 Feb 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc