Skip to content

Instantly share code, notes, and snippets.

@metruzanca
Created February 13, 2023 18:24
Show Gist options
  • Save metruzanca/0513249bd01babc780a913506dbfd5d0 to your computer and use it in GitHub Desktop.
Save metruzanca/0513249bd01babc780a913506dbfd5d0 to your computer and use it in GitHub Desktop.
Using Proxy API to detect a change in an object and calling a function after the update.
function onChange(data, callback) {
return new Proxy(data, {
set(target, prop, value) {
target[prop] = value;
callback(target, value);
return true;
}
});
}
const target = {
message1: "hello",
message2: "everyone",
};
const proxy = onChange(target, (target, value) => {
console.log(target, value);
})
proxy.message1 = 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment