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.

createViewModel doesn't work correctly with setters for computed values

See original GitHub issue

In my model I have several computed getters and accordingly setters, to derive the plain values from.

Simple example:

import { computed, observable } from 'mobx';

export default class Car {

  @observable public colorCode = 'red';

  @computed
  public get colorInstance(): any {
    return { colorCode: this.colorCode };
  }

  public set colorInstance(colorInstance: any) {
    this.colorCode = colorInstance.colorCode;
  }
}

If I now create a view model from it using createViewModel, changes applied to the colorCode property are directly visible in the view. If I instead apply changes by using the colorInstance setter, it seems to have no effect. See here for the full, running example: https://codesandbox.io/s/reverent-dew-pb6mh

When looking at the generated getters and setters in the code, the observation makes sense: computed values are always read from localComputedValues, while changes are always written to localValues. Hence, in my example colorInstance is written to the localValues colorInstance but read from localComputedValues, where it will never be up-to-date.

Is this a bug or is it intended to work like that and I made a mistake somewhere?

If you need further details, feel free to ask.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
ahoislcommented, Dec 14, 2020

I fixed this issue independently in one of my projects, then saw this issue and now did go forward and merged my implementation with the proposed fix in this thread. See and please review the open PR.

Additionally, if one is interested in a “deep” view model, one that is also able to transparently work with nested objects and arrays, then I could go forward and try to merge my private implementation into the mobx-utils, too…

1reaction
alex3683commented, Oct 25, 2019

So another solution would be to completely forbid the usage of getters / setters in view models…

Please don’t. I use them a lot to transform string references into instances of other models and back. I think this is one of the greatest mobx features (including all the others 😉)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Computed Setters don't update the View? - Stack Overflow
Computeds only update when reactive values are updated. Cookie values are not reactive, so the get doesn't know when there's a new value....
Read more >
Simplify Your Vue Components with Computed Setters - Medium
Let's take an example that at first doesn't use computed setters, and then improve it by introducing computed setters to it.
Read more >
Patterns - WPF Apps With The Model-View-ViewModel Design ...
After filling in the data entry form with valid values and clicking the Save button, the new customer's name appears in the tab...
Read more >
Properties — The Swift Programming Language (Swift 5.7)
Stored properties store constant and variable values as part of an ... If a computed property's setter doesn't define a name for the...
Read more >
Getter & Setter Computed Properties - Vue.js 2.0 ... - YouTube
This Vue.js tutorial covers computed property getter and setter ... Computed properties remember their values without needing to run a ...
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