You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
expected behaviour is this.count in this.renderCell will be 123, because this.renderCell was decorated with action.bound. But when I use reference on decorated function context of class is loose for this function.
For "mobx": "4.4.2" it worked correctly. But when we migrate our project on 6 version the bug appeared.
This is because the property items is initialized before makeObservable(this), which applies the decorator and binds the method. So when items is initialized, this.renderCell is not yet bound and what's more it's not even action yet.
Either move the initialization after makeObservable:
or bind them by yourself render: this.renderCell.bind(this)
or user arrow fn render: (...args) => this.renderCell(...args)
More importantly you shouldn't apply action to render functions - action applies untracked, so the observables accessed in renderCell won't be tracked. actions are intended for state mutations and the main goal is to batch these mutations.
This is because the property items is initialized before makeObservable(this), which applies the decorator and binds the method. So when items is initialized, this.renderCell is not yet bound and what's more it's not even action yet. Either move the initialization after makeObservable:
or bind them by yourself render: this.renderCell.bind(this) or user arrow fn render: (...args) => this.renderCell(...args)
More importantly you shouldn't apply action to render functions - action applies untracked, so the observables accessed in renderCell won't be tracked. actions are intended for state mutations and the main goal is to batch these mutations.
expected behaviour is
this.count
inthis.renderCell
will be123
, becausethis.renderCell
was decorated withaction.bound
. But when I use reference on decorated function context of class is loose for this function.For "mobx": "4.4.2" it worked correctly. But when we migrate our project on 6 version the bug appeared.
May be you can explain this behaviour.
https://codesandbox.io/s/mobx-action-bound-bug-ulred?file=/src/App.tsx
The text was updated successfully, but these errors were encountered: