-
-
Notifications
You must be signed in to change notification settings - Fork 350
MobX and Cannot update a component from inside the function body #846
Comments
Well, to be honest, that looks like a nice foot gun to by dynamically creating a component based on props. You are avoiding a bunch of optimizations that React provides without any good reason from what I can see. Why do you consider for example this approach as wrong? const ChildObserver = observer(({ foo }) => {
return <div>foo: {foo}</div>
})
@observer
class ParentObserver extends React.Component {
render() {
return (
<div>
<ChildObserver foo={this.props.foo} />
</div>
)
}
} |
Oh actually, it's silly altogether because that component is not even recreated, it's initialized once with the initial value of those props and it will never get updated. So I am curious what is your expected gain from this pattern of yours. |
I'm curious why you say this. Mobx-react makes class components have observable props: This in turn makes the Otherwise how does the count in the codesandbox increment? |
Yeah, you are right, I keep forgetting about that specific perk of a class observer. Functional components don't have this. Either way, I still don't see any benefit of your pattern over the one I inlined. |
Maybe we should remove that perk of the class observers? Didn't an early release of I don't think that'd fix the issue, but at least it'd remove this footgun |
No, we are certainly not going to remove that now and break a bunch of the other code. I will keep this open for now if someone else wants to chime in, but this is really such an odd pattern that I would recommend you to either stick to V5 or refactor it to something supported. |
If we would remove this perk, ChildComponent wouldn't have a way to update itself, so it would be failing silently anyway. You can get similar effect with computed (or @observer
class Component extends React.Component {
@computed
get getChildren() {
return <div>{this.props.foo}</div>;
};
render() {
return (
<div>
this.getChildren()
</div>
);
}
} |
Alright, I guess there is no path forward here, so let's close this. Hopefully, there will be a time where component classes will be a thing of the past and these hacks won't be necessary anymore. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions. |
Intended outcome
The linked component is a very common pattern around our (large) codebase. You have some class component, that defines a member property that accessing the parent component's props.
I'm not entirely sure this is solveable, but thought we should open an issue nonetheless.
With the upgrade to the most recent version of
react
we've seen some new errorHow to reproduce the issue
Codesandbox
https://codesandbox.io/s/thirsty-raman-w5pmo
https://codesandbox.io/s/eloquent-sky-sbo23
Actual outcome
Click the button, open the console, and you'll see a warning from react:
In facebook/react#18178 (comment), Dan suggests that it could be a library issue - In this case, there's a lot of mobx stuff.
Broadly it seems that clicking the button causes
the class component to update (
updateClassInstance
)This updates the observable props (
set
)This then triggers the Child component to re-render, as its reactions runs.
I do think this is also maybe one of those: 'bad patterns' that is mentioned in the thread.
Related issue: facebook/react#18178
The text was updated successfully, but these errors were encountered: