basic deep watch

main
Matthew Huntington 11 months ago
parent 76778071c9
commit a5c1a735e1

@ -1,9 +1,23 @@
const watch = (value, callback)=>{ const watch = (target, callback)=>{
return new Proxy(value, { set: callback }) for(const key in target){
if(typeof target[key] === 'object'){
target[key] = watch(target[key], callback)
}
}
return new Proxy(target, { set: callback })
} }
const state = watch({ name: 'matt' }, (newValue) => { const state = watch(
console.log(newValue); {
}) name: 'matt',
friend: {
name: 'bob'
}
},
(target, prop, newValue) => {
console.log(newValue);
}
)
state.name = 'Matthew' //state.name = 'Matthew'
state.friend.name = 'Bilbo'

Loading…
Cancel
Save