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.

Computed properties execute without being accessed

See original GitHub issue

Hi @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:

https://github.com/ctrlplusb/easy-peasy/blob/71ad696f3780a1d67cc8c865e56db3d661d0cb9d/src/extract-data-from-model.js#L172

I see a correct behavior but of course my understanding of the code is pretty limited.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ctrlplusbcommented, Oct 27, 2020

Will be in next release. 👍

0reactions
ctrlplusbcommented, Oct 22, 2020

Thanks for the feedback @mkuklis - very helpful to hear your own real world experience.

I reviewed our source code (around 30 models) and I haven’t seen a single case where computed prop is used inside the action. I did find one case where computed prop was used inside another computed prop (probably not a right thing to do anyway).

This should be ok still 🙏

Read more comments on GitHub >

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

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