Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Class constructor {class} cannot be invoked without 'new'

When running a create-react-app app with react-bootstrap installed, I am getting this error below when trying to add a Button

TypeError: Class constructor Button cannot be invoked without 'new'

My imports are like this:

import 'bootstrap/dist/css/bootstrap.min.css';
import './App.css';
import PropTypes from "prop-types";
import { Button } from "bootstrap";
import { PureComponent } from "react";

and the render code:

  render() {
    return (
      <div className="App">
        <header />
        <Button>Test</Button>
        ...

I have looked into lots of other questions talking about Babel, Webpack, and a compiler config. But I am not seeing all these configurations inside my create-react-app.

like image 914
Wagner Bertolini Junior Avatar asked Oct 24 '25 17:10

Wagner Bertolini Junior


2 Answers

So, I found the problem. After 1 hour lost. The mistake was on the suggested import that I've added.

Wrong:

import { Button } from "bootstrap";

Right:

import { Button } from "react-bootstrap";
like image 71
Wagner Bertolini Junior Avatar answered Oct 27 '25 07:10

Wagner Bertolini Junior


In my case the error was due the way I export my class.

At the end of my class file I added export default ClassName() instead of just export default ClassName without parenthesis.

like image 22
Eduardo Teixeira de Souza Avatar answered Oct 27 '25 05:10

Eduardo Teixeira de Souza