-
Notifications
You must be signed in to change notification settings - Fork 642
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
afterCreate is firing when a computed accesses a child causing a side effect #967
Comments
pretty sure I'm having the same issue. |
Actually the error is "correct". get selectedAnswers() {
return self.questions.reduce((result, question) => {
if (question.answers.every(a => !a.selected))
question.answers[0].selected = true
result.push(question.answers.find(a => a.selected));
return result;
}, []);
} But I totally agree that such behaviour is not expected and quite unobvious. This happens due to
First At the moment I can not figure out any way to fix this without breaking change (i.e. async |
Oh, so this is derived from the fact that starting from MST3 afterCreate doesn't fire until the instance is observed, right? Maybe computed values should be allowed to fire afterCreates. After all, afterCreates should only fire once. |
@k-g-a thanks for the great explanation! And thanks for looking into the issue. I'll try to work around this for the time being. |
Right. MST3 introduced lazy instances creation. |
Not ideal but I managed to work around it by wrapping the call to the action (inside afterCreate) that does the mutations in a setTimeout. Seems to have fixed the errors |
I ended up working around this by making explicit calls to preconfig actions after creation. Not great, but it will keep me moving. |
@k-g-a, mobx exposes an internal api, |
@mweststrate I tried to fix it using that but it will still throw, I think it is because of this:
the check that throws the exception completely ignores globalState.allowStateChanges, which is what is set by _allowStateChanges I guess that if would need to be changed inside mobx to
before it can be fixed? |
or maybe creating a whole new |
just created a PR on mobx for it |
@xaviergonz ok sorry, should have read above comments before reading the PR on mobx, lol :). Ok, I'm going to check what the impact of that check would be, adding another global state modifier wrapping function doesn't make it easier to grasp :) |
@xaviergonz thanks for picking that up! Any update on this? |
still waiting on the PR on the mobx side to resolve before this one can be addressed I'm afraid |
…d value (#1706) * make allowStateChanges allow changes inside computed value in order to fix MST mobxjs/mobx-state-tree#967 * added allowStateChangeInsideComputed * added unit test * small unit test error * safer implementation * Alternative solution * Revert "Alternative solution" This reverts commit e15bc78. * improve unit test * Changed to more predictable restoration
…side a computed value (#1706) * make allowStateChanges allow changes inside computed value in order to fix MST mobxjs/mobx-state-tree#967 * added allowStateChangeInsideComputed * added unit test * small unit test error * safer implementation * Alternative solution * Revert "Alternative solution" This reverts commit e15bc78. * improve unit test * Changed to more predictable restoration
@xaviergonz released! MobX 4.5.0 / 5.5.0 includes the change |
@mweststrate and there's the PR :) |
should be fixed in 3.5.0, but only if mobx >=4.5.0 || >= 5.5.0 is installed as well |
Thank you!
…On Sat, Sep 29, 2018, 10:58 AM Javier Gonzalez ***@***.***> wrote:
should be fixed in 3.5.0, but only if mobx >=4.5.0 || >= 5.5.0 is
installed as well
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#967 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAIrcqaYluKJhgYqw1Z_Qq0GUt37QRcPks5uf5iPgaJpZM4V8jRQ>
.
|
FYI: I've posted this on spectrum. It looks like a possible bug.
Here's a small (50 ln) codesandbox example showing what I'm up against:
https://codesandbox.io/s/94zy6n96jo
My issue is with a tree where a child has an afterCreate action that modifies another child. If I access the child data using a computed I get the following error:
Error: [mobx] Computed values are not allowed to cause side effects by changing observables that are already being observed. Tried to modify: [email protected]
I assumed afterCreate would fire after the object had been created? Can anyone shed some light on when afterCreate and afterAttach really fire?
Thanks!!
The text was updated successfully, but these errors were encountered: