JSFiddle - React, Tailwind, and code Playground
by nimareq
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router-dom/4.3.1/react-router-dom.min.js"></script>
<div id="app"></div>
React
const Router = ReactRouter
const Link = Router.Link
var App ({
mixins: [Router.State],
componentDidMount: function() {
},
render: function() {
var name = this.getRoutes().reverse()[0].name;
return (
<div>
<ul>
<li><Link to="view1">View1 link</Link></li>
<li><Link to="view2">View2 link</Link></li>
</ul>
<RouteHandler key={name}/>
</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</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" handler={View1}/>
<Route name="view2" handler={View2}/>
</Route>
);
Router.run(routes, function (Handler) {
React.render(<Handler/>, document.getElementById('app'));
});