mobx icon indicating copy to clipboard operation
mobx copied to clipboard

Upgrading Mobx from 5.15.7 to 6 breaks React class components

Open alesn opened this issue 6 years ago • 8 comments

Intended outcome: After upgrading from last Mobx 5 version (5.15.7) to Mobx 6 (applies to all versions, including 6.0.2) my project still works the same as it did.

Actual outcome: After the upgrade of Mobx - (updating mobx from 5.15.7 to 6.0.0, 6.0.1 or 6.0.2 is the only change done) my React application crashes at start when it attempts to render initial React component tree throwing an error: TypeError: Class constructor Router cannot be invoked without 'new'. ('Router' is the first React class component encountered when rendering initial React component tree. If I change this class component into a functional component, the error occurs at the next class component encountered.)

How to reproduce the issue: Reproducing the issue is not easy. This is a larger Meteor.js (latest version) project in Typescript. Also Babel is involved (though minimizing babel configuration to bare minimum that allows the project to build didn't change the result in any way). I was wondering if - from the description of the issue and changes made in Mobx 6 from the last 5 version you could think of something that could cause a problem like this. Thank you.

Versions From 5.15.7 upgrading to 6

alesn avatar Nov 02 '20 22:11 alesn

I suppose you haven't read a migration guide ... sigh. If that doesn't help you, you need to figure the reproduction. We can't possibly help you if we don't know your setup. Also, you haven't said version your mobx-react, that needs to be upgraded as well.

danielkcz avatar Nov 02 '20 22:11 danielkcz

I have read the migration guide and didn't find anything related to this. I thought someone in the know of the internal changes in mobx in version 6 could have an idea what involves class components specifically and changed in a way that could break class component functionality when installing Mobx version 6

alesn avatar Nov 02 '20 22:11 alesn

A major portion of changes was how observables are created, not how they are observed. Also, check the compatibility table.

However, as I said, without some guidelines on what you are doing, we can't help you. I would recommend you to just start from scratch, forget about Meteor or fancy tools and simply replicate the smallest possible portion of your app that uses MobX and is not working.

danielkcz avatar Nov 03 '20 06:11 danielkcz

Along mobx I am using mobx-react and mobx-utils. I was using versions "mobx": "5.15.7", "mobx-react": "6.3.0", "mobx-utils": "5.6.1", With these versions the problem doesn't occur. I have used the compatibility table you mentioned before and was using mobx-react 6 with mobx 5 without a problem. Upgrading them to mobx 5.15.7 → 6.0.2 mobx-react 6.3.0 → 7.0.0 mobx-utils 5.6.1 → 6.0.1 this problem occurs. The character of the problem is not that after the upgrade some of mobx's fuctionality wouldn't work correctly (e.g. observables wouldn't be observed) but that upgrading mobx to version 6, I guess "breaks the build" so that React class components (which don't use mobx or any other mobx package) get malformed or something, so they are not instantiable.

Though I understand the mobx packages need to be used with "matching versions" of each other, seeing the problem looks like the upgrade is causing a problem already in the "build" stage that the project after the upgrade is not built correctly, I tried pin-pointing upgrading which of the mobx packages triggers this problem. Upgrading mobx-react and mobx-utils mobx-react 6.3.0 → 7.0.0 mobx-utils 5.6.1 → 6.0.1 and keeping mobx at 5.15.7 didn't trigger this problem. Upgrading mobx 5.15.7 → 6.0.2 (or to 6.0.0) did trigger this problem.

Researching this problem I have found this error (TypeError: Class constructor cannot be invoked without 'new'.) reported in relation to Next.js or webpack and Babel and some changes to build system configuration were reported to help in some cases. Though I am not sure how upgrading one npm package (mobx) in a system that built and worked correctly before should trigger build system malfunctioning ... I'll try to investigate deeper

alesn avatar Nov 03 '20 10:11 alesn

I don't think there is value in keeping this open for now, more research will lead hopefully to either discovering what is wrong, or a new very specific issue report. Closing this one for now.

mweststrate avatar Nov 03 '20 21:11 mweststrate

Hi, we have a similar bug. @alesn did you finally find a solution to your problem?

gcattan avatar Jul 25 '22 21:07 gcattan

Hi gcattan, it was a long time ago, so I don't remember the details but I believe it was a long process of trying removing certain blocks of npm packages figuring out what's affecting the result and finally found it. Sorry can't be more helpful

alesn avatar Jul 26 '22 07:07 alesn

Ok, I understand. Thank you for answering.

gcattan avatar Jul 26 '22 08:07 gcattan