You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constobj=observable.box({a: 0,b: {x: 0,y: 0}});autorun(()=>{console.log("X",computed(()=>obj.get().b.x).get());});autorun(()=>{console.log("Y",computed(()=>obj.get().b.y).get());});obj.set({a: 0,b: {x: 0,y: 0}});// no changeobj.set({a: 1,b: {x: 0,y: 0}});// no change to X or Yobj.set({a: 1,b: {x: 1,y: 0}});// change to Xobj.set({a: 1,b: {x: 2,y: 1}});// change to X and Yobj.get().b={x: 2,y: 1};// no changeobj.get().b={x: 2,y: 2};// change to Y
In the above example, X and Y values only get logged when their values change, regardless even if the entire object was swapped. But to achieve this behavior I need to call computed on every single property access. Is it possible to make it so all property accesses are by default computed? This way the above code can be re-written in the following way;
The reason why I want this is because I have an app where the whole state can be completely swapped out to a completely different state (imagine a call to structuredClone) but the new state is structurally similar. Meaning, without adding computeds everywhere, this swap would cause the entire app to re-render when maybe a property or two have actually changed.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
In the above example,
X
andY
values only get logged when their values change, regardless even if the entire object was swapped. But to achieve this behavior I need to callcomputed
on every single property access. Is it possible to make it so all property accesses are by defaultcomputed
? This way the above code can be re-written in the following way;The reason why I want this is because I have an app where the whole state can be completely swapped out to a completely different state (imagine a call to
structuredClone
) but the new state is structurally similar. Meaning, without addingcomputed
s everywhere, this swap would cause the entire app to re-render when maybe a property or two have actually changed.Beta Was this translation helpful? Give feedback.
All reactions