-
Notifications
You must be signed in to change notification settings - Fork 643
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
mutations on array do not lead to rerender #1060
Comments
This is the page,
|
That is more about mobx/mobx-react than mobx-state-tree Basically what happens is this Premises:
(json.stringify commented out)
(json.stringify not commented)
Your best bet is either: There are of course more solutions, but those two are the simplest ones that come to my mind |
Thanks for your reply, you are so kind. |
Sorry I was looking at StandardTable still Table seems to have lots of custom logic to achieve something similar |
Another way is to do |
I created an minimal reproduction codesandbox for testing these cases. |
If you add |
Maybe you can try
and then use OTable instead of Table and that will work without having to make snapshots, but that very much depends on the implementation of Table itself |
I had similar problem, I think the last suggestion is the best - yes, it's not pretty. You could also always pass a new array to Table component as data.slice() and then rely on rows keys for React reconciler to update only new rows. In my case I changed the ”Table” component API to something like https://material-ui.com/demos/tables/, where you need to loop through all rows in your render function (which means it will be tracked by mobx). |
Ok, I understand this is a feature instead of a bug now. |
Glad you got it solved :) |
Since these components are passed an observable array, they won't re-render automatically when the array's internals update, and instead only re-render when a primitive value updates. This means that their rendering is always a step behind in the simulation. This makes the less pure-componenty, but seems to be the recommended way. See: mobxjs/mobx-state-tree#1060 https://github.com/mobxjs/mobx-react#faq mobxjs/mobx#101 An alternative would be to use `getSnapshot(data)`, but this is claimed to be less efficient. (Untested, and perhaps for our use-case is negligible and the cleaner code would be preferred.)
@zmayor can you explain what you mean by "it is a feature now"? I have run into this problem. Now as a hacky hack to trigger rerenders, we add stuff like.
To all our mutating functions. This can't be intended right? Also, the link to your reproduction is dead now. Can you post it again? I would be very interested in it |
@schumannd that is definitely not how it is supposed to be done. But please don't comment on issues that are closed for over a year, but rather file a new bug report. |
Bug report
Sandbox link or minimal reproduction code
reproduction
Describe the expected behavior
When I change the array, the page should rerender.
Describe the observed behavior
When I push some item into an array, the array changed, but there is no rerender. When I change the text in the same model, the rerender happend. What's wrong with these code?
The text was updated successfully, but these errors were encountered: