Replies: 4 comments 3 replies
-
I'm rusty on class components, but I'm not sure you need to wrap
For inheratince reason it might be that you need to protect the render function, so you end up with something like:
|
Beta Was this translation helpful? Give feedback.
-
Hello, In previous mobx-react versions, up until v7, it was possible to inherit from observer parent classes and redeclare the children as observers. Overwritting the render function was working correctly, and it also did not trigger any errors, as shown in this example: In newer mobx-react v9 however, the exact same code throws the following error: As you mentioned, if we dont override the render function directly and instead use a renderImpl, it works correctly without the need to re-declare the child as observer. Are there any other solutions where we can avoid doing this? Thanks for your feedback! |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Thank you both! For the time being we will proceed with exporting 2 instances of each component, ideally however we would like a solution within the library itself to avoid this. We'll check mobx-react source code a bit in the next weeks and try to come up with ideas/solutions or at least some tests in order in order to have clear view of the problem. |
Beta Was this translation helpful? Give feedback.
-
Hello,
We're using MobX and MobX-react in a couple of very large scale projects. One of the projects includes a list of React components for UI elements such as inputs, dropdowns, etc. that are being used by the others.
These components can be either used "as is" so they are wrapped by Observer HoC, or be imported and extended in the rest of the projects to modify their behavior and render functions depending on their needs.
The problem with MobX-react 9.x.x is that this is strictly not allowed, see:
https://github.com/mobxjs/mobx/blob/main/packages/mobx-react/src/observerClass.ts#L62
In previous version this was simply a warning, see:
https://github.com/mobxjs/mobx-react/blob/master/src/observerClass.ts#L25
Seems like the warning and tracking of components that are already observables was added in response to: mobxjs/mobx-react#839
In even older versions of MobX-react this could be accomplished through "wrappedComponent" property available in wrapped components with Observer to access the original component.
I've raised a similar issue in the past to address a warning for our project use cases here:
mobxjs/mobx-react@b095407#diff-b97ac37a763a2cc028e2da311ed32ce9b4941b59a39b1ae619f9f335d9526749
And this test is a good example of we are using the components:
mobxjs/mobx-react@b095407#diff-ebddfbd06fdc3c5239aa8f53eb90b15da8c6f940b3214edbe685fafff1ceae88
Are there any ways in the latest version to achieve this?
Ideas that come into my mind to resolve this:
Monkey patch MobX-react, by overwriting how "makeClassComponentObserver" works and not throw when parsing components being already observers.
Have a utility method in MobX-react that can "undo" an observer component and restore to its normal state? This could be done either inside Mobx-react itself, when it detects that a component is already an observer or in user-space. For example, in components that extending a component which is already an observer:
export default observer(convertToNormalComponent(ObservableBaseField));
Any feedback would be much appreciated!
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions