React router example
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/JSXTransformer.js"></script>
<script src="https://npmcdn.com/react-router/umd/ReactRouter.min.js"></script>
<script src="https://fb.me/react-dom-15.0.1.js"></script>
<script src="https://fb.me/react-with-addons-15.0.1.js"></script>
<div id="app"></div>
Babel + JSX
var Router = ReactRouter;
var Route = Router.Route,
DefaultRoute = Router.DefaultRoute,
Link=Router.Link,
RouteHandler = Router.RouteHandler,
hashHistory = Router.hashHistory;
var App = React.createClass({
mixins: [Router.State],
componentDidMount: function() {
},
render: function() {
return (
<div>
<ul>
<li><Link to="view1/ippon">View1 link</Link></li>
<li><Link to="view2">View2 link</Link></li>
</ul>
<RouteHandler />
</div>
);
}
});
var View1 = React.createClass({
componentWillLeave: function(cb) {
console.log('View1 will leave');
setTimeout(cb, 1);
},
componentDidMount: function(cb) {
console.log('View1 will open');
},
render: function() {
return (
<div>View 1 content {this.props.params.name}</div>
);
}
});
var View2 = React.createClass({
componentWillLeave: function(cb) {
console.log('View2 will leave');
setTimeout(cb, 1);
},
componentDidMount: function(cb) {
console.log('View2 will open');
},
render: function() {
return (
<div>View 2 content</div>
);
}
});
var routes = (
<Route name="app" path="/" handler={App}>
<Route name="view1" path="/view1/:name" handler={View1}/>
<Route name="view2" handler={View2}/>
</Route>
);
ReactDOM.render((
<Router history={hashHistory}>
<Route name="app" path="/" handler={App}>
<Route name="view1" path="/view1/:name" handler={View1}/>
<Route name="view2" handler={View2}/>
</Route>
</Router>
), document.getElementById('app'))