Socket
Book a DemoInstallSign in
Socket

on-change

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

on-change

Watch an object or array for changes

latest
Source
npmnpm
Version
6.0.0
Version published
Weekly downloads
523K
-18.75%
Maintainers
1
Weekly downloads
 
Created

What is on-change?

The on-change package is a utility for watching changes in JavaScript objects and arrays. It allows you to execute a callback function whenever a change is detected, making it useful for state management, debugging, and reactive programming.

What are on-change's main functionalities?

Watch for changes in objects

This feature allows you to watch for changes in an object's properties. When a property changes, the callback function is triggered, providing the path of the changed property, the new value, and the previous value.

const onChange = require('on-change');

const object = {
  foo: 'bar'
};

const watchedObject = onChange(object, function (path, value, previousValue) {
  console.log('Object changed:', path, value, previousValue);
});

watchedObject.foo = 'baz'; // Console: Object changed: foo baz bar

Watch for changes in arrays

This feature allows you to watch for changes in arrays. The callback function is triggered whenever the array is modified, such as when elements are added or removed.

const onChange = require('on-change');

const array = [1, 2, 3];

const watchedArray = onChange(array, function (path, value, previousValue) {
  console.log('Array changed:', path, value, previousValue);
});

watchedArray.push(4); // Console: Array changed: 3 4 undefined

Nested property change detection

This feature allows you to detect changes in nested properties of objects. The callback function provides the full path to the changed property, making it easy to track changes deep within an object structure.

const onChange = require('on-change');

const nestedObject = {
  level1: {
    level2: {
      foo: 'bar'
    }
  }
};

const watchedNestedObject = onChange(nestedObject, function (path, value, previousValue) {
  console.log('Nested object changed:', path, value, previousValue);
});

watchedNestedObject.level1.level2.foo = 'baz'; // Console: Nested object changed: level1.level2.foo baz bar

Other packages similar to on-change

Keywords

on

FAQs

Package last updated on 23 Sep 2025

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