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.

Misleading documentation on destructuring compted properties

See original GitHub issue

Hey guys,

First of all, thank you very much for the great library!

I must caveat this is 100% nit, but maybe you will find it useful to correct – happy to provide a docs PR if you suggest so.

I have been playing around with various aspects of it and noticed that the docs at https://easy-peasy.dev/docs/api/computed.html#computed-properties-break-when-destructuring-a-computed-property-out-of-state, although being correct in general, provide an example that actually is not flawed.

The thing is that when a computed property getter is pure and depends solely on the sub-state it’s defined in (just like in the example where isLoggedIn only changes when user of session sub-model changes), the deconstruction will work just fine. The comparison function in useStoreState will receive the whole sub-model state, will notice it changed and will trigger a re-render, leading to computed property working correctly.

What will break though is if a computed property is impure w.r.t to the sub-model it’s defined in (e.g. depends on another sub-model). It’s not straightforward to achieve (the default use of computed helper supplies local state only), but of course possible when state resolver is involved:

const storeModel = {
  user: {
    current: null,
  },
  session: {
    isLoggedIn: computed((_, storeState) => [storeState.user], (userState) => userState.current != null),
  },
};

The reason I am writing about this at all is because it’s very convenient to use the deconstruction in certain cases and it’s a pity the documentation effectively bans that for computed property – but I appreciate it might be perceived easier than mentioning specific conditions under which it is ok or not ok.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
yardcommented, Sep 22, 2022

Yeah that makes sense, let me write it up 😃

0reactions
jmyrlandcommented, Sep 22, 2022

Thanks again!👏

Read more comments on GitHub >

github_iconTop Results From Across the Web

Destructuring assignment - JavaScript - MDN Web Docs
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from ...
Read more >
Destructuring index signature by computed property loses ...
You've said that test has an index signature; you're basically ignoring properties on the RHS and guaranteeing that any property on test is...
Read more >
Object Destructuring in ES6 - DEV Community ‍ ‍
Computed property name is another object literal feature that also works for destructuring. You can specify the name of a property via an ......
Read more >
Why vue computed function could destructuring assignment ...
First i think when function didn't get argument, would take "this" be the argument. see below. but is wrong. so why computed didn't...
Read more >
10. Destructuring - Exploring JS
Destructuring is a convenient way of extracting multiple values from data stored in (possibly nested) objects and Arrays. It can be used in...
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