question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Watch a single property without "auto"

See original GitHub issue

Is 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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
aleclarsoncommented, Sep 22, 2021

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.

import { watch } from 'wana'

const watcher = watch(state, 'count', change => {
  console.log('new count:', change.value)
})

watcher.dispose()
1reaction
ryanking1809commented, Nov 25, 2019

Oh I meant an array of properties to react to. eg. (perhaps a better api)

const state = o({ count: 0, foo: 0, bar: 0 })
const observer = onChange([[state, 'foo'], [state, 'bar']], change => {
  // Nothing is ever observed in here.
})
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found