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 even if they are not currently being accessed.

See original GitHub issue

Hi @ctrlplusb,

We ran into an issue with computed properties. It seems like computed properties which are currently not being accessed also execute which cause all sorts of weird problems.

Here is a simple example:


const userModel = {
  data: {},
  withBlocked: computed(
    [state => state.data],
    user => {
      // computed body
    },
  ),
  withFriends: computed(
    [state => state.data],
    user => {
      // computed body
    },
  ),
  authorize: action((state, payload) => {
    state.data = payload;
  }),
  unauthorize: action(state => (state.data = {})),
};

now somewhere else when we execute:

const actions = store.getActions();
actions.user.unauthorize();

both withBlocked and withFriends will be fired even though they are not used anywhere.

This maybe related to https://github.com/ctrlplusb/easy-peasy/issues/446

I started to look at the source code and I think the issue could be related to: https://github.com/ctrlplusb/easy-peasy/blob/master/src/create-reducer.js#L48

Although this probably requires a bigger refactor in order to track which computed props are currently utilized.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
ctrlplusbcommented, Mar 30, 2020

Thanks for the feedback both. I am investigating. 👍

2reactions
ctrlplusbcommented, Mar 27, 2020

Hi @mkuklis

I have already tracked this down. ☺️

A fix is in the works. It has to do with the way immer is working. I will be pushing a patch onto the 3.4.0 alpha branch soonish and will ping you to ask you to test it. 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't I access to my data function from my computed ...
This works fine but it's not the job of the computed property which should be used to return some stuff based on other...
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 >
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 >
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 >
Interesting bug / quirk computed property not updating ...
Computed properties are cached, and only re-computed on reactive dependency changes. Note that if a certain dependency is out of the instance's ...
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