I am new to react and am trying build a simple react app. I want to show a component based on the current route.
index.js
var React = require('react');
var ReactDOM = require('react-dom');
var createReactClass = require("create-react-class");
import { Router, Route, browserHistory, Link } from "react-router";
var About = require("./about");
var Admin = require("./admin");
var App = createReactClass({
render: function() {
return(
<Router history={browserHistory}>
<Route path={"/"} component={RootComponent}></Route>
<Route path={"/about"} component={About}></Route>
<Route path={"/admin"} component={Admin}></Route>
</Router>
);
}
});
// Create a component
var RootComponent = createReactClass({
render: function() {
var currentLocation = this.props.location.pathname;
return(
<div>
<Link to={"/about"}>About</Link>
<Link to={"/admin"}>Admin</Link>
<h2>This is the root component</h2>
</div>
);
}
});
// put component into html page
ReactDOM.render(<App />, document.getElementById("app_root"));
Is it possible to put an if else
condition to render something on a layout something like the code below?
var RootComponent = createReactClass({
render: function() {
var currentLocation = this.props.location.pathname;
if (currentLocation == "admin") {
return(
<div class="admin_template">
</div>
);
} else {
return(
<div class="home_template">
</div>
);
}
}
});
I would like to detect the route change when a link is clicked then show template accordingly.
Any help is greatly appreciated. Thanks