Sandrina Pereira Sandrina Pereira - 1 year ago 451
Javascript Question

Using React Router with CDN

Resume: I need to run React Router without wepback or similar tools. Directly from CDN links, but I'm stuck with some require.js error.

I'm starting to build my first App with React and I'm struggling with React Router.


<div id="container"></div>

<script src=""></script>
<script src=""></script>
<script src=""></script>

<script src=""></script>

<script type="text/babel" src="assets/scripts/03_templates/app.js" charset="utf-8"></script>



var { Router, Route, IndexRoute, hashHistory, IndexLink, Link, browserHistory } = ReactRouter;

//some classes

<Router history={hashHistory}>
<Route path="/" component={Window}>
<IndexRoute component={InitialPage}/>
<Route path="register" component={Register} />
<Route path="search" component={Search} />
), document.getElementById("container"));

Everything is running fine but i get this on console:

react.js:3639 Warning: You are manually calling a React.PropTypes
validation function for the
prop on
. This
is deprecated and will not work in production with the next major
version. You may be seeing this warning due to a third-party PropTypes

So, I suppose my react Router is a old version. I changed the link to

<script src=""></script>

Warning: React.createElement: type should not be null, undefined,
boolean, or number. It should be a string (for DOM elements) or a
ReactClass (for composite components).

I search about it and it seems the problem is on line 1. So I changed this:

var { Router, Route, IndexRoute, hashHistory, IndexLink, Link, browserHistory } = ReactRouter;

To this:

import { Router, Route, IndexRoute, hashHistory, IndexLink, Link, browserHistory } from 'react-router';

And now I have this problem:

app.js:2 Uncaught ReferenceError: require is not defined

I searched for require.js, tried some stuff but nothing fixed my problem. What am I missing? I need to run this without webpack or similars tools.


Answer Source

Use this on top of your javascript:

var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var IndexRoute = ReactRouter.IndexRoute;
var Link = ReactRouter.Link;
var browserHistory = ReactRouter.browserHistory;

and remove the import statements.

I'm currently using this react-router package:


Here's an example at CodePen: -- it uses no import statements.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download