Getting “Cannot call a class as a function” in my React Project

后端 未结 30 720
無奈伤痛
無奈伤痛 2020-12-07 16:34

I\'m trying to add a React map component to my project but run into an error. I\'m using Fullstack React\'s blog post as a reference. I tracked down where the error gets thr

相关标签:
30条回答
  • I received this error by making small mistake. My error was exporting the class as a function instead of as a class. At the bottom of my class file I had:

    export default InputField();
    

    when it should have been:

    export default InputField;
    
    0 讨论(0)
  • 2020-12-07 16:43

    I have also run into this, it is possible you have a javascript error inside of your react component. Make sure if you are using a dependency you are using the new operator on the class to instantiate the new instance. Error will throw if

    this.classInstance = Class({})

    instead use

    this.classInstance = new Class({})

    you will see in the error chain in the browser

    at ReactCompositeComponentWrapper._constructComponentWithoutOwner

    that is the giveaway I believe.

    0 讨论(0)
  • 2020-12-07 16:44

    In my case i wrote comment in place of Component by mistake

    I just wrote this.

    import React, { Component } from 'react';
    
      class Something extends Component{
          render() {
              return();
         }
      }
    

    Instead of this.

    import React, { Component } from 'react';
    
      class Something extends comment{
          render() {
              return();
         }
      }
    

    it's not a big deal but for a beginner like me it's really confusing. I hope this will be helpfull.

    0 讨论(0)
  • 2020-12-07 16:46

    Two things you can check is,

    class Slider extends React.Component {
        // Your React Code
    }
    
    Slider.propTypes = {
        // accessibility: PropTypes.bool,
    }
    
    • Make sure that you extends React.Component
    • Use propTypes instead of prototype (as per IDE intellisense)
    0 讨论(0)
  • 2020-12-07 16:46

    For me, it was because I'd accidentally deleted my render method !

    I had a class with a componentWillReceiveProps method I didn't need anymore, immediately preceding a short render method. In my haste removing it, I accidentally removed the entire render method as well.

    This was a PAIN to track down, as I was getting console errors pointing at comments in completely irrelevant files as being the "source" of the problem.

    0 讨论(0)
  • 2020-12-07 16:48

    Looks like there're no single case when this error appears.

    Happened to me when I didn't declare constructor in statefull component.

    class MyComponent extends Component {
    
        render() {
            return <div>Test</div>;
        }
    }
    

    instead of

    class MyComponent extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            return <div>Test</div>;
        }
    }
    
    0 讨论(0)
提交回复
热议问题