In this project, we will create a very basic React blog application that makes
use of multiple routes. We will be using the react-router-dom package to
assist us with creating different routes in this application.
Forkandclonethis repository.- Run
npm install.
In this step we will install the routing package we need and setup our router.
- Run
npm install react-router-dom. - Open
App.js. - Import
HashRouterfromreact-router-dom. - Wrap the
HashRoutercomponent around the existingdiv.
src/App.js
import React, { Component } from 'react';
import { HashRouter } from 'react-router-dom';
import Nav from './Components/Nav/Nav';
import './App.css';
class App extends Component {
render() {
return (
<HashRouter>
<div className="App">
<Nav />
<h1 style={{ padding: '200px 35%' }}>
This is where your pages will appear
</h1>
</div>
</HashRouter>
);
}
}
export default App;In this step, we will create our routes using the existing components.
- Create a new file,
routes.jsinsrc. - Import
React. - Import
SwitchandRoutefromreact-router-dom. - Import two of our view components,
HomeandTopicList. - Export by default a
Switchcomponent. - Add two
Routecomponents inside theSwitchcomponent, one for both the views we imported.- Each route should have a
pathand acomponentprop. - Home should have a
pathof'/'.- Home should have the
exactprop
- Home should have the
- TopicList should have a
pathof'/topics'.
- Each route should have a
src/routes.js
import React from 'react';
import { Switch, Route } from 'react-router-dom';
import Home from './Components/Home/Home';
import TopicList from './Components/TopicList/TopicList';
export default () => (
<Switch>
<Route exact path="/" component={Home} />
<Route path="/topics" component={TopicList} />
</Switch>
);In this step, we'll render the routes inside the main App.js component.
- Open
App.js. - Import the
routes.jsfile. - Delete the entire
h1element. - Underneath the
Navcomponent use{}tointerpolate(insert) and render the imported router.
src/App.js
import React, { Component } from 'react';
import { HashRouter } from 'react-router-dom';
import routes from './routes';
import Nav from './Components/Nav/Nav';
import './App.css';
class App extends Component {
render() {
return (
<HashRouter>
<div className="App">
<Nav />
{routes}
</div>
</HashRouter>
);
}
}
export default App;In this step, we will setup navigation to the routes we just created.
- Open
Nav.js. - Import
Linkfromreact-router-dom. - Wrap a
Linkcomponent around the content of the twolitags.- The Home
lishould link to'/'. - The Topics
lishould link to'/topics'.
- The Home
src/Components/Nav/Nav.js
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import './Nav.css';
class Nav extends Component {
render() {
return (
<div className="Nav">
<div>
<img
src="https://boom.camp/wp-content/uploads/2019/04/logo-boomcamp.png"
alt="boomcamp logo"
/>
</div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/topics">Topics</Link>
</li>
</ul>
</div>
);
}
}
export default Nav;In this step, we'll add another route that uses parameters.
- Open
routes.js. - Import our final view component,
Post. - Add an additional
Routecomponent to ourSwitch.- It should have a
pathof'/post/:id'.
- It should have a
src/routes.js
import React from 'react';
import { Switch, Route } from 'react-router-dom';
import Home from './Components/Home/Home';
import TopicList from './Components/TopicList/TopicList';
import Post from './Components/Post/Post';
export default () => (
<Switch>
<Route exact path="/" component={Home} />
<Route path="/topics" component={TopicList} />
<Route path="/post/:id" component={Post} />
</Switch>
);In this step, we'll change our Post view to use the route parameter to display
the correct blog post.
- Open
Post.js. - Update the
findmethod incomponentDidMountto use theparamsdata instead of being hard-coded to the number 2.- Remember routing data, like parameters, is found on props.
- IMPORTANT: parameters are strings (because they are a part of the URL). The ids of our posts are numbers, so be careful when comparing them.
src/Components/Post/Post.js
...
componentDidMount() {
// This is where you would make an axios call to a server in a fullstack application
// but for today we'll be just be filter over an array of dummy data
let post = posts.find(post => post.id === parseInt(this.props.match.params.id));
this.setState({
title: post.title,
content: post.content
});
}
...In this step, we'll change our TopicList view to link to the Post view.
- Open
TopicList.js. - Import
Linkfromreact-router-dom. - Wrap a
Linkcomponent around the content of thelitag found in the.map. - The
lishould link to'/post/'+ the id of the post.
src/Components/TopicList/TopicList.js
import React, { Component } from "react";
import { Link } from "react-router-dom";
import posts from "./../../post_data.json";
import "./TopicList.css";
...
let displayTopics = posts.map(post => {
return (
<li key={post.id}>
<Link to={`/post/${post.id}`}>{post.title}</Link>
</li>
);
});
...