Uncaught TypeError: Cannot read property 'createClass' of undefined
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;
Looks like you have
var DefaultRoute = React.createClass({ ..
and then overwrites this with
var DefaultRoute = Router.DefaultRoute;
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.
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 :)