Watch a single property without "auto"
See original GitHub issueIs there a way to apply watch
just to specific object key rather than the whole object?
I have a large observable state, and need to trigger some side effect when certain properties change. The side effect doesn’t use the object I’m watching so I can’t use auto
, and watch
on the whole state will cause the side effect to fire unnecessarily.
A simple example below, is it possible to log "side effect"
to the console when count
changes but not when number
changes?
https://codesandbox.io/s/serene-frog-5uige
const state = o({
count: 0,
number: 0
});
watch(state.count, () => console.log("unrelated side effect"));
function App() {
return (
<div className="App">
<button onClick={() => state.count++}>Count ++</button>
<button onClick={() => state.number--}>Number --</button>
</div>
);
}
EDIT: Perhaps the best way is to just to reference the value in auto
and never use it?
auto(() => {
const watch = state.count;
console.log("unrelated side effect")
})
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
VueJS deep watcher - specific property on multiple objects
I have a "products" array with multiple objects. Each product object contains the property "price". I want to watch this property in each ......
Read more >Watchers | Vue.js
Watchers declared using the watch option or the $watch() instance method are automatically stopped when the owner component is unmounted, so in most...
Read more >$watch multiple properties for single callback #844 - GitHub
Based on @denwerboy's solution - pass the watched property name to the callback function. Useful to save changes to localStorage automatically. Vue.prototype.$ ...
Read more >Vue's watch vs watchEffect, which should I use?
Let's unwrap the core differences between watch and watchEffect so that you can use the best tool for the job in your day-to-day...
Read more >Vue.js 3 $watch and $watchEffect - Medium
It automatically unwraps the ref object and passes the value property to its handler function. This is why we got London rather than...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can now use
watch
to observe a single property. Just note that if the property points to an observable object, that entire object will also be observed.Oh I meant an array of properties to react to. eg. (perhaps a better api)