Reputation: 77
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.jsx';
import Home from './Home.jsx';
import Contact from './Contact.jsx';
import About from './About.jsx';
import { BrowserRouter as Router, Route, IndexRoute } from 'react-router-dom';
import {history } from 'history';
ReactDOM.render( (
<Router history = {history}>
<Route path = "/" component = {App}>
<IndexRoute component = {Home} />
<Route path = "/home" component = {Home} />
<Route path = "about" component = {About} />
<Route path = "contact" component = {Contact} />
</Route>
</Router>
), document.getElementById('app'));
import React from 'react';
import ReactDOM from 'react-dom';
import { Link } from 'react-router-dom';
class App extends React.Component {
render() {
return (
<div>
<ul>
<li><Link to = "/home">Home</Link></li>
<li><Link to = "/about">About</Link></li>
<li><Link to = "/contact">Contact</Link></li>
</ul>
{this.props.children}
</div>
)
}
}
export default App;
import React from 'react';
import ReactDOM from 'react-dom';
class About extends React.Component {
render() {
return (
<div>
<h1>About...</h1>
</div>
)
}
}
export default About;
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<title>React App</title>
</head>
<body>
<div id = "app"></div>
<script src = "index.js"></script>
</body>
</html>
The code renders in browser. - Home - About - Contact
Question :- When i click the link About the url changes to (http://localhost:8080/about) on the browser link. But the About.jsx page is not shown. I do not know the reason why? Kindly Help.
Upvotes: 0
Views: 546
Reputation: 1006
You forgot to prefix your route names with a slash (/
).
UPDATE: also, remove the .jsx
extension from your import paths. See below:
import App from './App';
import Home from './Home';
import Contact from './Contact';
import About from './About';
ReactDOM.render( (
<Router history = {history}>
<Route path = "/" component = {App}>
<IndexRoute component = {Home} />
<Route path = "/home" component = {Home} />
<Route path = "/about" component = {About} />
<Route path = "/contact" component = {Contact} />
</Route>
</Router>
), document.getElementById('app'));
Upvotes: 2