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
Mobx Array when using push #2326
Comments
Please, try to minimize that code to show an actual issue at hand or ideally setup CodeSandbox. It's unclear what problem should |
The code is uploaded to But this error cannot be reproduced in codesandbox |
You're probably iterating over an array and mutating it at the same time. |
Thank you for your reply I do n’t think it ’s because of the indexing problem you mentioned. Maybe can change the type of ‘arr’ in forEach to ‘proxy’ so that there will be no problem when operating |
I see, but assuming that you cannot modify the array during iteration (due to the problems explained above), then it shouldn't matter that the third argument isn't the actual proxied array, because it's for reading only. I mean even if the Isolated reproduction of possible issue: const observableArray = observable([1,2,3]);
observableArray.forEach((item,index,arr) => {
console.log(observableArray === arr); // false
}); Culprit mobx/src/v5/types/observablearray.ts Lines 474 to 475 in 8a63a29
|
Thanks for your reply, I understand the problems in my previous understanding. |
It doesn't matter which method you use, you shouldn't modify the array (more specifically it's length) during the iteration, because then you're risking that you will skip or re-do some elements, depending on how the iteration protocol is implemented. Some languages even throw an exception is such situations: https://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html |
Confirming this is a bug: The 3rd argument passed to |
I get this error when i use array.push
mobx.module.js:3138 Uncaught Error: [mobx] Modification exception: the internal structure of an observable array was changed.
at ObservableArrayAdministration.updateArrayLength (mobx.module.js:3138)
at ObservableArrayAdministration.spliceWithArray (mobx.module.js:3176)
at Proxy.push (mobx.module.js:3302)
at AppState.js:127
The Source code is
I think the reason is ' data.forEach '. The item in the loop is not a proxy object ,but 'item.children' is a proxy object
So I replaced ‘data.forEach’ with the following function ,and no errors have occurred since
Is it a problem with foreach or am I using it wrong ?
the version i use is
The text was updated successfully, but these errors were encountered: