Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mobx computed value error #25

Closed
fusepilot opened this issue Nov 18, 2016 · 4 comments
Closed

Update mobx computed value error #25

fusepilot opened this issue Nov 18, 2016 · 4 comments

Comments

@fusepilot
Copy link

Reproduction: http://codepen.io/fusepilot/pen/mOOgWX

When deserializing back to a mobx computed value using deserializeState() in example 7, I get this error:

Invariant failed: [ComputedValue 'Box@1870.area'] It is not possible to assign a new value to a computed value.

I was expecting update to ignore setting computed values. But maybe that requires serializr to have too much knowledge of mobx's inner workings. Not sure.

Is this a bug? Or is there a proper way to avoid this problem when updating?

@mweststrate
Copy link
Member

Conceptually, one should usually not need to serialize computed values. Since it is derived data there is often no need to store it. But if you want to serialize it, giving it a setter that doesn't do anything (or does actually something useful), should fix your problem, e.g.:

@serializable @computed get prop() { 
  return expr
}
set prop(v) {
  // ignore updates during de-serialization
}

@fusepilot
Copy link
Author

Then the problem becomes:

Uncaught Error: [mobx] Invariant failed: @observable properties cannot have a setter.

http://codepen.io/fusepilot/pen/eBvMMK

Conceptually, one should usually not need to serialize computed values

Right, but it is useful when you just want to flush the entire state including computed values to json and not have to duplicate the computed function in the code receiving it.

@mweststrate
Copy link
Member

What mobx version are you using? I think setters for computed properties were only introduced in 2.6 or later

@fusepilot
Copy link
Author

Yeah, the codepen I was using for the reproductions was using 2.0.0. Updated to 2.6.3 and it works. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants