How it works...

The match object contains a lot of useful information. React Router also includes the object's history and location. As you can see, we can get all the parameters we pass within our routes in the match object.

If you run the application and go to /notes URL, you will see this view:

If you click on any link (I clicked on My note 2), you will see this view:

After this, we can add a menu in our Header component to access all our routes:

import React from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import logo from '../../images/logo.svg';

// We created a component with a simple arrow function.
const Header = props => {
const {
title = 'Welcome to React',
url = 'http://localhost:3000'
} = props;

return (
<header className="App-header">
<a href={url}>
<img src={logo} className="App-logo" alt="logo" />
</a>
<h1 className="App-title">{title}</h1>

<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/notes">Notes</Link></li>
<li><Link to="/contact">Contact</Link></li>
</ul>
</header>
);
};

// Even with Functional Components we are able to validate our PropTypes.
Header.propTypes = {
title: PropTypes.string.isRequired,
url: PropTypes.string
};

export default Header;
File: src/shared/components/layout/Header.jsx

After that, we need to modify our src/components/App.css file to style our menu. Just add the following code at the end of the CSS file:

.App-header ul {
margin: 0;
padding: 0;
list-style: none;
}

.App-header ul li {
display: inline-block;
padding: 0 10px;
}

.App-header ul li a {
color: #fff;
text-decoration: none;
}

.App-header ul li a:hover {
color: #ccc;
}
File: src/components/App.css
Now you can see the menu like this:
 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset