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

后端 未结 30 722
無奈伤痛
無奈伤痛 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条回答
  • 2020-12-07 16:56

    You have duplicated export default declaration. The first one get overridden by second one which is actually a function.

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

    In file MyComponent.js

    export default class MyComponent extends React.Component {
    ...
    }
    

    I put some function related to that component:

    export default class MyComponent extends React.Component {
    ...
    }
    
    export myFunction() {
    ...
    }
    

    and then in another file imported that function:

    import myFunction from './MyComponent'
    ...
    myFunction() // => bang! "Cannot call a class as a function"
    ...
    

    Can you spot the problem?

    I forgot the curly braces, and imported MyComponent under name myFunction!

    So, the fix was:

    import {myFunction} from './MyComponent'
    
    0 讨论(0)
  • 2020-12-07 16:57

    For me it happened when I forgot to write extends React.Component at the end. I know it's not exactly what YOU had, but others reading this answer can benefit from this, hopefully.

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

    Happened to me because I used

    PropTypes.arrayOf(SomeClass)

    instead of

    PropTypes.arrayOf(PropTypes.instanceOf(SomeClass))

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

    Mostly these issues occur when you miss extending Component from react:

    import React, {Component} from 'react'
    
    export default class TimePicker extends Component {
        render() {
            return();     
        }
    }
    
    0 讨论(0)
  • 2020-12-07 17:02

    I faced this error when I imported the wrong class and referred to wrong store while using mobx in react-native.

    I faced error in this snippet :

    import { inject, Observer } from "mobx-react";
    
    @inject ("counter")
    @Observer
    

    After few corrections like as below snippet. I resolved my issue like this way.

    import { inject, observer } from "mobx-react";
    
    @inject("counterStore")
    @observer
    

    What was actually wrong,I was using the wrong class instead of observer I used Observer and instead of counterStore I used counter. I solved my issue like this way.

    0 讨论(0)
提交回复
热议问题