Computed properties execute without being accessed
See original GitHub issueHi @ctrlplusb, apologies for reporting this again but I’m still seeing some issues with computed properties executing by default in v4.0.1. I spent a bit of time and I was able to track it down. It basically happens when you have more than one model with action on one model and computed prop on another. When the action is executed and changes model’s local state the computed prop on another model also executes. Here is a simple test to illustrate this:
test('computed properties should not execute when not accessed', () => {
let computedCount = 0;
const store = createStore({
user: {
firstName: 'Mary',
lastName: 'Poppins',
fullName: computed(
[(state) => state.firstName, (state) => state.lastName],
(firstName, lastName) => {
computedCount += 1;
return `${firstName} ${lastName}`;
},
),
setFirstName: action((state, payload) => {
state.firstName = payload;
}),
},
group: {
data: {},
change: action((state) => {
state.data.somedata = 1;
}),
}
});
store.getActions().group.change();
// assert which currently fails
expect(computedCount).toBe(0);
});
I noticed that when I turn this line off:
I see a correct behavior but of course my understanding of the code is pretty limited.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Understanding computed properties in Vue.js - LogRocket Blog
Computed properties cache results, meaning a computed property only executes once after the component is mounted except for any of its ...
Read more >Computed Properties - Vue.js
A computed property will only re-evaluate when some of its reactive dependencies have changed. This means as long as author.books has not changed,...
Read more >swift - Are computed properties evaluated every time they are ...
In the example below I created a Computed Property age . As you can see each time I invoke it, the code in...
Read more >Computed Properties - The Object Model - Ember Guides
A computed property will only recompute its value when it is consumed. Properties are consumed in two ways: By being accessed, for example...
Read more >What are computed properties in Swift and when should you ...
Keep in mind that a computed property is evaluated every time you access it. This means that its value is always up to...
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 Free
Top 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

Will be in next release. 👍
Thanks for the feedback @mkuklis - very helpful to hear your own real world experience.
This should be ok still 🙏