64
var React = require('react');

module.exports=React.createClass({
   render:function(){
   return(
        <div>
           <h1> the list  </h1>
        </div>   
   )}
})

When I run the above code I get the following error :

app.js:4 Uncaught TypeError: React.createClass is not a function

Is this because of a version difference or a typo ?

package.json-I have included create-react-class as seen here but not in the bower.json file 


 {
    "dependencies": {
        "browser-sync": "^2.18.13",
        "browserify": "^14.4.0",
        "create-react-class": "^15.6.2",
        "ejs": "^2.5.7",
        "express": "^4.16.0",
        "gulp": "^3.9.1",
        "gulp-live-server": "0.0.31",
        "react": "^16.0.0",
        "reactify": "^1.1.1",
        "vinyl-source-stream": "^1.1.0"
      }
    }

gulpfile.js -Am I missing some dependency in this file

var gulp= require('gulp');
var LiveServer= require('gulp-live-server');
var browserSync=require('browser-sync');
var browserify = require('browserify');
var reactify= require('reactify');
var source = require('vinyl-source-stream');

gulp.task('live-server', function(){

    var server= new LiveServer('server/main.js');
    server.start();
})

gulp.task('bundle',function(){
  return browserify({
    entries:'app/main.jsx',
    debug:true,
  })
  .transform(reactify)
  .bundle()
  .pipe(source('app.js'))
  .pipe(gulp.dest('./.tmp'))

})


gulp.task('serve',['bundle','live-server'],function(){
    browserSync.init(null,{
        proxy: "http://localhost:7777",
        port:9001
    })
})

And my main.jsx has the following

 var React = require('react');
var createReactClass = require('create-react-class');

var GroceryItemList=require('./components/GroceryItemsList.jsx');

React.render(<GroceryItemList/>,app);

the groceryitems.jsx has the following

var React = require('react');
var createReactClass = require('create-react-class');
module.exports=React.createReactClass({

        render:function(){
            return(
                <div>
                    <h1> Grocery Listify </h1>
                </div>

            )

        }
})    

When I add the createReactClass I get an error: createReactClass is not a function and when I add import and ES6 syntax I get 'illegal import deceleration ' Thank you,

Naveen

Naveen DINUSHKA
  • 1,497
  • 3
  • 19
  • 37
  • Wouldn't there be a problem by trying to export an unnamed React component? btw, you should probably go with the updated and very well supported ES6 class syntax in the future. – Andrew Sep 29 '17 at 06:10
  • I did that and I ended up with my browser totally frozen ... strange ... – Felipe Mar 08 '18 at 21:54

7 Answers7

73

This is surely a version problem, If you are starting fresh, I would suggest you to create a React component by extending React.Component rather than using React.createClass since its deprecated from version 16.0 onwards and has been moved to a separate package 'create-react-class' as @Dream_Cap also mention

Also go with the ES6 syntax for imports. You would modify your code to

import React from 'react';

export default class App extends React.Component {
    render(){
       return(
           <div>
               <h1> the list  </h1>
           </div>   
        )
     }
}
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • I get this when I try Import Parse Error: Line 1: Illegal import declaration while parsing file: – Naveen DINUSHKA Sep 30 '17 at 03:11
  • 1
    try "const React = require('react')" – Dream_Cap Sep 30 '17 at 03:27
  • Excellent answer! But when I kept the `default` it was throwing error: `...does not contain an export named ...`. Then I used `export class ...` without the `default`. Can you help me understand the need of `default` here? – arshovon Dec 13 '17 at 05:57
  • 1
    @arsho, check this https://stackoverflow.com/questions/42051886/default-keyword-in-import-export-es6/42051990#42051990 – Shubham Khatri Dec 13 '17 at 06:12
66

Per the documents, you need to get the npm create react class package. From the command line of your project's file path, you need to do npm install create-react-class --save, and then modify the code like this:

var React = require('react');
//You need this npm package to do createReactClass
var createReactClass = require('create-react-class');

    module.exports=createReactClass({
            render:function(){
                return(
                    <div>
                        <h1> the list  </h1>
                    </div>   
                )
            }

You also need React DOM to render the components like this:

npm install react react-dom
var ReactDOM = require('react-dom');
ReactDOM.render(<GroceryItemList/>,app);
jmargolisvt
  • 5,722
  • 4
  • 29
  • 46
Dream_Cap
  • 2,292
  • 2
  • 18
  • 30
  • 1
    I get this error on the console 'createReactClass' is not a function – Naveen DINUSHKA Sep 30 '17 at 03:13
  • Did you do "npm install create-react-class --save" from the command line to install the package? – Dream_Cap Sep 30 '17 at 03:26
  • if we use module.exports=createReactClass it wont give the error createReactClass' is not a function – Naveen DINUSHKA Oct 01 '17 at 02:11
  • I still get an error ... basically in the console I get something like the function, let's say: Greeter, hasn't being declared ... strange ... – Felipe Mar 08 '18 at 05:43
  • I got the same error, but after `npm install create-react-class`, I now get an error on the server side ```node_modules/client-sessions/lib/client-sessions.js:259 var components = content.split("."); ^ TypeError: Cannot read property 'split' of undefined at Object.decode (.../project/node_modules/client-sessions/lib/client-sessions.js:259:28) at Namespace.io.sockets.on.socket (.../project/server.js:62:27) ...[nodemon] app crashed - waiting for file changes before starting...``` I dont understand what is going on. This happens no matter how I do it. – John Feb 13 '20 at 22:56
  • yeah this was the best answer BY FAR – Droid Chris Dec 03 '21 at 21:44
21

If you're getting an Error stating that React.creatClass is an undefined function it's because the newer versions of React don't support that.

You can try the below steps.

Install the create-react-class npm package:

npm install --save-dev create-react-class

Then create a new variable under the ReactDom variable:

var createReactClass = require('create-react-class');

Now instead of using React.createClass() for the components use the createReactClass() var.

Example: Replace this:

var TodoComponent = React.createClass({
    render: function() {
        return(`<h1>`Helloooo`</h1>`);
    }
});

With this:

var TodoComponent = createReactClass({
    render: function() {
        return(`<h1>`Helloooo`</h1>`);
    }
});
3

Thanks for all the help .This is how the final answer looks like: 1) use react-dom to render

 var ReactDOM = require('react-dom');
  var List=require('./components/List.jsx');
  ReactDOM.render(<List/>,app);

2) use create-react-class (not react) and export it 'module.exports= createReactClass({.... instead of module.exports=React.createReactClass (this gives an error 'createReactClass' is not a function

Thank you all again! '

Naveen DINUSHKA
  • 1,497
  • 3
  • 19
  • 37
1
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

const App = function(){};
App.prototype = Object.create(Component.prototype);
App.prototype.render = function(){
     return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to React</h1>
        </header>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
     )
   }
export default App;

In this way you could avoid Class keyword

0

React.createClass is not listed in updated documentation, You have to go for createComponent with ECMA6 Class

0

Try npm install first it could be that your devependencies in package.json is not fully installed.

zero8
  • 1,997
  • 3
  • 15
  • 25