I am new to ReactJS. I need to have a common header and change title according to the route changes. Do I need to create a header.jsx
file and import it? Or else, how can I render the header (common file) with route?
My routing part looks like this:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.jsx';
import Home from './Home.jsx';
import { Router, Route, Link, browserHistory, IndexRoute } from 'react-router';
ReactDOM.render((
<Router history = {browserHistory}>
<Route path = "/home" component = {Home} />
<Route path = "/" component = {App}>
</Route>
</Router>
));
So if you need to display a common header among your routes, there's a couple ways of doing it. One is you can define your header inside its own component. Something simple for example:
Then in your home component, app component, etc. Simply put inside your render(), after importing it at the top of each file.
The other option is to create your own sort of container component, still using the Header component we defined above:
Then where you declare your routes:
Admittedly i have not tried that second option. You might have to pass the router as a parameter from the container component down to its use of children component, if you want to do things like
router.transitionTo('/path')
.It's just an option if you don't want to repeat everywhere.
try this https://www.npmjs.com/package/react-helmet
This should work:
header.jsx:
first-page.jsx:
second-page.jsx
app.jsx:
web-app.jsx: