react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Uncaught TypeError: Cannot read property 'createClass' of undefined

Open mhd-sln opened this issue 11 years ago • 3 comments

I have been trying to use react router in one of my application. But am getting

Uncaught TypeError: Cannot read property 'createClass' of undefined

on line

var DefaultRoute = React.createClass({ ..

The code I have been writing is

var Q                       = require('q'), 
    doThis                  = require('./generic/modules/doThis.js'),
    doThat                  = require('./generic/modules/doThat.js');
var React = require('react');
var Router = require('react-router');
var Route = Router.Route;
var Redirect = Router.Redirect;
var DefaultRoute = Router.DefaultRoute;

var aModule = require('./modules/aModule/aModule.js');
var Master = require('./modules/master/master.js');

var injectTapEventPlugin = require('react-tap-event-plugin');

(function (){

    var routes = (
        <Route name="root" path="/" handler={Master}>
            <Route name="time" handler={aModule} />
        </Route>);
    window.React = React;
    injectTapEventPlugin();

    var doThisPromise = doThis.init();
    doThisPromise.
    then(function() {
        doThat.init();
        Router.run (routes, function(Handler) {
            console.log('in router run');
            React.render(<Handler/>, document.body);
        });
    }, function (err) {

    });
})();

If necessary please ask and I will also provide the route related js

-- EDIT -- The relevant error code from the build file

},{}],84:[function(require,module,exports){
var React = (window.React);
var FakeNode = require('../mixins/FakeNode');
var PropTypes = require('../utils/PropTypes');

/**
 * A <DefaultRoute> component is a special kind of <Route> that
 * renders when its parent matches but none of its siblings do.
 * Only one such route may be used at any given level in the
 * route hierarchy.
 */

var DefaultRoute = React.createClass({

  displayName: 'DefaultRoute',

  mixins: [ FakeNode ],

  propTypes: {
    name: React.PropTypes.string,
    path: PropTypes.falsy,
    handler: React.PropTypes.func.isRequired
  }

});

module.exports = DefaultRoute;

mhd-sln avatar Feb 06 '15 17:02 mhd-sln

Looks like you have

var DefaultRoute = React.createClass({ ..

and then overwrites this with

var DefaultRoute = Router.DefaultRoute;

waldreiter avatar Feb 06 '15 17:02 waldreiter

How do you bundle your files? Seems like React is undefined in react-router, because the line window.React = React is executed only after require('react-router'), and you somehow configured react-router to access react via window.React.

agundermann avatar Feb 06 '15 18:02 agundermann

Looks like you're using the dist/ build with webpack. Don't include the stuff from dist, just simply npm install react-router and then var Router = require('react-router') and that's it :)

ryanflorence avatar Feb 07 '15 07:02 ryanflorence