314

I am using ReactJS.

When I run the code below the browser says:

Uncaught TypeError: Super expression must either be null or a function, not undefined

Any hints at all as to what is wrong would be appreciated.

First the line used to compile the code:

browserify -t reactify -t babelify examples/temp.jsx  -o examples/public/app.js

And the code:

var React = require('react');

class HelloMessage extends React.Component {
  render() {
    return <div>Hello </div>;
  }
}

UPDATE: After burning in hellfire for three days on this problem I found that I was not using the latest version of react.

Install globally:

sudo npm install -g [email protected]

install locally:

npm install [email protected]

make sure the browser is using the right version too:

<script type="text/javascript" src="react-0.13.2.js"></script>

Hope this saves someone else three days of precious life.

11
  • 112
    As of 0.14.8, you can still get this if you do something like extends React.component (lowercase c). Commented Jun 7, 2016 at 21:45
  • 14
    @Kevin just want to rephrase , basically If you have a typo there somewhere , in my case it was Components instead of Component :). Your comment helped BTW
    – P-RAD
    Commented Aug 14, 2016 at 14:02
  • 2
    I did React.Components (plural), the right is React.Component (singular) Ow good... how did i miss that...
    – Ismael
    Commented Jun 7, 2017 at 13:06
  • 1
    This can also happen if you have recursive imports. i.e ComponentA requires ComponentB and ComponentB requires ComponentA.
    – Snowman
    Commented Oct 3, 2017 at 18:21
  • 5
    @Kevin Suttle You comment is actually more useful than the answer
    – Mick Jones
    Commented May 2, 2018 at 15:21

47 Answers 47

370

Class Names

Firstly, if you're certain that you're extending from the correctly named class, e.g. React.Component, not React.component or React.createComponent, you may need to upgrade your React version. See answers below for more information on the classes to extend from.

Upgrade React

React has only supported ES6-style classes since version 0.13.0 (see their official blog post on the support introduction here.

Before that, when using:

class HelloMessage extends React.Component

you were attempting to use ES6 keywords (extends) to subclass from a class which wasn't defined using ES6 class. This was likely why you were running into strange behaviour with super definitions etc.

So, yes, TL;DR - update to React v0.13.x.

Circular Dependencies

This can also occur if you have circular import structure. One module importing another and the other way around. In this case you just need to refactor your code to avoid it. More info

11
  • 212
    For everyone else that has this issue but updating React is not the fix - scroll further down to the other answers, it might be a simple typo. In my case it was a class-definition using React.component instead of React.Component Commented Jul 11, 2016 at 14:23
  • 1
    FYI, old way class can be extended also with extends. Try this var x = function(){}; class y extends x {}
    – Mouneer
    Commented Dec 19, 2016 at 16:18
  • 2
    It was a circular import structure for me. Thanks for saving me a lot of hours of debugging! Commented Oct 12, 2017 at 12:39
  • 4
    Just FYI. I recently got the same error despite being on ReactJS 16.x. Turns out I had a typo in this line: class App extends React.Component () {...} - which should be corrected to class App extends React.Component {...} (without the () at the end)
    – Atlas7
    Commented Nov 10, 2017 at 10:55
  • 1
    Capital 'C' Component! #facepalm
    – David
    Commented Nov 16, 2017 at 14:04
136

This means that you want subclass something, which should be Class, but is undefined. The reasons might be:

  • typo in Class extends ..., so you extending undefined variable
  • typo in import ... from, so you import undefined from module
  • referenced module does not contain the value, you want import (e.g. obsolete module - case with React), so you importing non existing value (undefined)
  • typo in referenced module export ... statement, so it exports undefined variable
  • referenced module missing export statement at all, so it exports just undefined
10
  • 23
    In my case it was a lowercase of Component in React.Component.
    – Union find
    Commented Aug 16, 2016 at 23:52
  • 3
    In my case it was a self written class not correctly imported. I was importing the default exported class via import {Foo} from 'bar' instead of import Foo from 'bar'. Upvoted therefore.
    – xtra
    Commented Sep 9, 2016 at 6:16
  • 5
    Don't do this either: class Thing extends React.Component() { -- I had to remove the () Commented Dec 24, 2016 at 1:23
  • In my case this error was caused by accidentally creating a circular reference by what I was importing(the class I wanted to extend to the subclass) in my components render function. When I tried to execute/render the subclass since the superClass wasnt created yet it was undefined.
    – Leon
    Commented Jan 10, 2017 at 0:30
  • ^ Caused by a circular reference for me as well.
    – Cailen
    Commented Jan 13, 2017 at 21:29
94

It can also be caused by a typo error, so instead of Component with capital C, you have component with lower c, for example:

React.component //wrong.
React.Component //correct.

Note: The source of this error is may be because there is React and we think automatically what comes next should be a react method or property starting with a lowercase letter, but in fact it is another Class(Component) which should start with a capital case letter.

1
  • 3
    huh, strangely this error isn't caught during the webpack build step, but it will show up at run time.
    – worc
    Commented Dec 7, 2017 at 23:09
33

In my case, with react-native, the error was that I had

import React, {
  AppRegistry,
  Component,
  Text,
  View,
  TouchableHighlight
} from 'react-native';

instead of

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Image,
  AsyncStorage
} from 'react-native';
1
  • 2
    This worked in my case! The documentation provided in react-native site has this buggy example :)
    – theusual
    Commented Mar 14, 2018 at 5:25
22

I've seen this error when you have a circular dependency.

class A extends B {}
class B extends C {}
class C extends A {}
1
  • I was trying to extend an imported class and then re-export that extension as the original name of the original import. So that was probably it? Commented Feb 13 at 20:37
18

You can also receive this if you are attempting to execute React.Component with an erroneous () in your class definition.

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

Which I sometimes manage to do when I'm converting from a stateless functional component to a class.

1
  • THIS was the issue for me. very silly. thanks a lot!
    – bstst
    Commented Mar 18, 2019 at 15:31
16

I experienced this when missing an export statement for the JSX class.

For example:

class MyComponent extends React.Component {
}
export default MyComponent // <- add me
0
10

For any other persons, that may develop this issue. You could also check that the component method in React.Component is capitalized. I had that same issue and what caused it was that I wrote:

class Main extends React.component {
  //class definition
}

I changed it to

class Main extends React.Component {
  //class definition
}

and everything worked well

10

just add "use client" and it works for me, btw i use nextjs

7

Webpack 4 Chunks and Hashes with Uglification by TerserPlugin

This can occur when Webpack uses chunks and hashes with minification and unglification via TerserPlugin (currently on version 1.2.3). In my case the error was narrowed down to the uglification by the Terser Plugin of my vendors.[contenthash].js bundle which holds my node_modules. Everything worked when not using hashes.

Solution was to exclude the bundle in the uglification options:

optimization: {
  minimizer: [
    new TerserPlugin({
      chunkFilter: (chunk) => {
        // Exclude uglification for the `vendors` chunk
        if (chunk.name === 'vendors') {
          return false;
        }
        return true;
      },
    }),
  ],
}

More info

2
  • This indeed solved the problem for me, however I was able to pin point the culprit and so at the end I was able to apply the uglification. See my answer - react-dazzle.
    – Erez Cohen
    Commented Mar 4, 2019 at 9:27
  • I narrowed down to terser plugin, ultimately changing react-script to version 3.2.0 fixed the issue for me.
    – avck
    Commented Jan 16, 2020 at 15:18
7

I had a similar issue with Next.js 13 and tried all the solutions above but none worked for me. I had to change the component to a client component before the error disappeared.

6

I am going to contribute another possible solution, one that worked for me. I was using the convenience index to collection all components into one file.

I don't believe at the time of writing this is officially supported by babel, and throws typescript into a spin - however I've seen it used in many projects and is definitely convenient.

However, when used in combination with inheritance it seems to throw the error presented in the question above.

A simple solution is, for modules that act as parents need to be imported directly instead of via a convenience index file.

./src/components/index.js

export Com1 from './com-1/Com1';
export Com2 from './com-2/Com2';
export Com3 from './com-3/Com3';
export Parent1 from './parent/Parent1';

./src/components/com-1/Com1.js

import { Com2, Com3 } from '../index';

// This works fine
class Com1 {        
    render() {
        return (
            <div>
                <Com2 {...things} />
                <Com3 {...things} />
            </div>
        );
    }
}

./src/components/com-3/Com3.js

import { Parent } from '../index';

// This does _not_ work
class Com3 extends Parent {        
}

./src/components/com-3/Com3.js

import Parent from '../parent/Parent';

// This does work
class Com3 extends Parent {        
}
1
  • This helped me figure out what I did wrong. Accidentally wrapped the component name in { } in my import statement. Subtle difference. Can be hard to spot that mistake.
    – Dennis W
    Commented Mar 30, 2018 at 3:22
6

I got this when I had a typo on my import:

import {Comonent} from 'react';
2
  • I also got this with extends React.Components instead of React.Component (no s). Commented Jul 25, 2016 at 17:18
  • 1
    I also got this but for typing small first letter of a component - ... extends React.component {} Commented Sep 6, 2016 at 16:49
5

Check for the Classes you extend actually exists, few React methods are depreciated, It also might be a Typo error 'React.Components' instead of 'React.Component'

Good Luck!!

1
  • In my case I was using React.component instead of React.Component (notice the capital "C" I was missing) Commented Jul 10, 2017 at 3:20
4

I have same issue, just change from Navigator to {Navigator}

import Navigator from 'react-native-deprecated-custom-components'
// to
import {Navigator} from 'react-native-deprecated-custom-components'
1
  • 1
    basically it should match like this: export Foo ... import { Foo } - or - export default Foo ... import Foo
    – dpren
    Commented Aug 11, 2017 at 17:03
4

This worked for me:

import React, {Component} from 'react';
4

My conditions

  • Create-React-App
  • React-scripts v3.2
  • Froala rich text editor v3.1
  • Development mode worked fine
  • The production build was broken with the error mentioned in the question

Solution to my problem

Downgrade Froala to v3.0.

Something in v3.1 broke our Create React App build process.

Update: Use react scripts v3.3

We discovered that there was an issue between React Scripts 3.2 and Froala 3.1.

Updating to React Scripts v3.3 allowed us to upgrade to Froala 3.1.

4
  • 2
    I love you. I want to come to find you and kiss your feet !!!!!!!!!!!!!!!!!!!!!!!!! (Nightmare all-nighter over)
    – GaddBox
    Commented Feb 7, 2020 at 22:45
  • I'm not using create react app, but facing the same issue on prod with froala 3.1. Can you explain what was the issue here? Commented May 7, 2020 at 7:43
  • There's a project I have with the same issue only in production. This only happened after updating an internal library. No create-react-app and no froala. Curious what causes this issue to only occur in production. Do you know what was different? Commented Nov 16, 2020 at 18:30
  • I can't answer any of your questions. All I know is that under the conditions I had, doing what I said in my answer solved the problem for me. Commented Nov 16, 2020 at 22:57
3

Not correct for this answer but for others with same error my ridiculously silly mistake could potentially help.

Stupidly,my issue was using function notation by including () following the class name.

Make sure your syntax is correct. And you've imported & exported any external components/modules with the correct names and paths.

This template should work fine if you have a newish version of react installed:

import React, { Component } from 'react'

class ExampleClass extends Component {

    render(){
        return(
            <div>

            </div>
        )
    }
}

export default ExampleClass 
2

I`ve written

React.component

instead of React.Component That was my issue)) and was looking for this more than half an hour.

2

In my case I was using:

class Root extends React.Component() {
// THIS IS WORNG
// After React.Component () <- WRONG!!
}

which was wrong but correct is:

class Root extends React.Component {
// THIS IS CORRECT
// That -> '()' after React.Component was typo
}

also make sure there is
React.Component
NOT
ˣ React.component or React.Components

2
  • Welcome to SO. It seems clear the OP didn't commit this mistake as they have included the correct form already in the question. Were you getting the same error as OP but with a different cause?
    – Eduardo
    Commented Dec 4, 2017 at 21:04
  • Yes I was getting the same error as OP but I found that the cause was different and hope this will help others. Commented Dec 4, 2017 at 21:52
2

I experienced this error while importing component like:

import React, { Components } from 'react';

instead of

import React, { Component } from 'react';
2

There might be a third party package causing this. In our case it was react-dazzle. We have similar settings to that of @steine (see this answer above).

In order to find the problematic package I ran the webpack build locally with production mode and thus was able to find the problematic package in the stack trace. So for us this provided the solution and I was able to keep the uglification.

2

Change import React from 'react-dom to import React, {Component} from 'react'
And change class Classname extends React.Component to class Classname extends Component
If you are using the latest version of React(16.8.6 as of now).

1

Using Babel (5.8) I get the same error if I try to use the expression export default in combination with some other export:

export const foo = "foo"
export const bar = "bar"
export default function baz() {}
1

Happened to me too when I used this :

class App extends React.Component(){

}

Instead of the right one :

class App extends React.Component{

}

Notice:- () in the first one which is the main cause of this problem

1

Here is one answer:

import React, { Component } from 'react'; // NOT 'react-dom'
1
1

In my case, I fixed this error by change export default class yourComponent extends React.Component() {} to export default class yourComponent extends React.Component {}. Yes delete the parenthesis () fixed the error.

1

Look if you have a typo error in your importation or your class generation, it could be simply that.

1

I've seen this error occur due to 'comments' in the bundle generated by webpack. Using 'pathinfo'= true in webpack.config.js can cause this issue:

webpack.config.js

module.exports = {
  output: {
    pathinfo: true,
  }
}

'pathinfo' defaults to true in development and false in production mode. https://webpack.js.org/configuration/output/#outputpathinfo Try setting this value to false to resolve the issue.

This can also happen if you are not using a plugin to strip the comments from the build output. Try using UglifyJs (https://www.npmjs.com/package/uglifyjs-webpack-plugin/v/1.3.0):

webpack.config.js

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
  ...
  optimization: {
    minimizer: [new UglifyJsPlugin(
      new UglifyJsPlugin({
        uglifyOptions: {
          output: {
            comments: false
          }
        }
      }),
    )],
  }
}
0
1

There could be a spelling/case mistake in component name: For example:

class HelloMessage extends React.Component

class HelloMessage extends React.component

Please check.

Not the answer you're looking for? Browse other questions tagged or ask your own question.