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

后端 未结 30 961
無奈伤痛
無奈伤痛 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条回答
  •  Happy的楠姐
    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'
    

提交回复
热议问题