Skip to content
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

Class constructor <React Class component> cannot be invoked without 'new' error #3503

Closed
iicdii opened this issue Aug 17, 2022 · 4 comments
Closed
Labels

Comments

@iicdii
Copy link

iicdii commented Aug 17, 2022

Intended outcome:
No error is thrown when it renders React class component.

Actual outcome:
error

How to reproduce the issue:

  1. Go to https://codesandbox.io/s/beautiful-dream-kz0n75?file=/src/ToastStore.tsx
  2. Click "Push Toast" button
  3. Check out the error

Versions

  • mobx: 6.6.1
  • mobx-react: 7.5.2
  • react: 18.2.0
  • react-dom: 18.2.0
  • react-scripts: 5.0.1
@mweststrate
Copy link
Member

mweststrate commented Aug 17, 2022 via email

@iicdii
Copy link
Author

iicdii commented Aug 17, 2022

  1. I also doubt the transpiler so I found this suggestion to change compiler target from es5 to es6 (Stackoverflow)
// tsconfig.json
"complierOptions": {
  "target": "es6" // from "es5"
},

It does not solve the problem though. (reference: https://codesandbox.io/s/beautiful-dream-kz0n75?file=/tsconfig.json)

  1. React.creatElement(Toast, { label: toast }) instead of <Toast ../> doesn't work.

@mweststrate
Copy link
Member

mweststrate commented Aug 17, 2022 via email

@iicdii
Copy link
Author

iicdii commented Aug 18, 2022

That works like charm! Thanks for help @mweststrate
I will leave the code snippet for someone who's struggled the same problem.

import React, { ReactElement } from 'react';
import { makeObservable, observable } from 'mobx-react';

class ToastStore {
  constructor() {
    makeObservable(this, { toasts: observable.shallow });
  }

  toasts: ReactElement<IToast>[] = [];
}

@iicdii iicdii closed this as completed Aug 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants