-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add a decorator for observable properties with action-wrapped setter #964
Comments
This was discussed before. It is possible to use That being said, having setters become actions is an anti-pattern imho. The property access should not be an action. Instead, the function or method that changes the property is the one that should be an action. The only reason I can think of for wanting to wrap setters in |
I disagree, calling a setter can be equivalent to calling a method, is calling a setter from an event handler bad practice? |
Well, not really, but if you are calling the setter from the event handler, why not just make the event handler an action? |
Hadn't considered that, so far all our actions were kept in our store classes. Feels like breaking encapsulation if we were to annotate component event handlers with |
Closing this as it is a duplicate of #839. Also note that a custom |
Idea:
Currently if we want to use mobx's strict mode, we need to wrap any method that does the writing to an observable in an
@action
. This is all great as it shows the intent of the method and all, but it gets really frustrating when you have simple properties and suddenly need to implement custom setters or setFoo() methods which will be wrapped in an action.Suppose we have this class:
we turn on strict and now setting the property must be wrapped in an action, so we do:
(of course you could keep the property public, but then you're throwing away benefits of TS and just asking for a problem during the runtime)
Our neat and compact code just grew significantly and the more properties you have the worse it gets.
Instead, how about adding another decorator which would make it clear that setting the property will be wrapped in an action?
And yey, we're back to neat and compact code and can use strict mode. 🎉
The text was updated successfully, but these errors were encountered: