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

后端 未结 30 721
無奈伤痛
無奈伤痛 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:48

    This is a general issue, and doesn't appear in a single case. But, the common problem in all the cases is that you forget to import a specific component (doesn't matter if it's either from a library that you installed or a custom made component that you created):

    import {SomeClass} from 'some-library'

    When you use it later, without importing it, the compiler thinks it's a function. Therefore, it breaks. This is a common example:

    imports

    ...code...

    and then somewhere inside your code

    <Image {..some props} />

    If you forgot to import the component <Image /> then the compiler will not complain like it does for other imports, but will break when it reaches your code.

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

    For me it happened because I didn't wrap my connect function properly, and tried to export default two components

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

    In my case, I accidentally put component name (Home) as the first argument to connect function while it was supposed to be at the end. duh.

    This one -surely- gave me the error:

    export default connect(Home)(mapStateToProps, mapDispatchToProps)
    

    But this one worked -surely- fine:

    export default connect(mapStateToProps, mapDispatchToProps)(Home)
    
    0 讨论(0)
  • 2020-12-07 16:48

    Actually all the problem redux connect. solutions:

    Correct:

    export default connect(mapStateToProps, mapDispatchToProps)(PageName)
    

    Wrong & Bug:

    export default connect(PageName)(mapStateToProps, mapDispatchToProps)
    
    0 讨论(0)
  • 2020-12-07 16:49

    I experienced this when writing an import statement wrong while importing a function, rather than a class. If removeMaterial is a function in another module:

    Right:

    import { removeMaterial } from './ClaimForm';
    

    Wrong:

    import removeMaterial from './ClaimForm';
    
    0 讨论(0)
  • 2020-12-07 16:49

    For me it was a wrong import of a reducer in the rootReducer.js. I imported container instead of reducer file.

    Example

    import settings from './pages/Settings';
    

    But sure it should be

    import settings from './pages/Settings/reducer';
    

    Where settings directory contains following files actions.js, index.js, reducer.js.

    To check it you can log reducers arg of the assertReducerShape() function from the redux/es/redux.js.

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