by Sai gowtham

How to add a header, footer components to React router

In this tutorial, we are going to learn about creating and adding a common header, footer components to react-router with the help of an example.

This tutorial assumes that you already have some basic knowledge about react-router otherwise you can check out my React router tutorial.

First, we need to create two new files called header.js, footer.js in our src folder or components folder.

Now inside header.js add your react app navigation links like i have shown in the below code.

header.js
import React from "react";
import { NavLink } from "react-router-dom";
function Header() {
  return (
    <nav>
      <NavLink exact activeClassName="active" to="/">        Home
      </NavLink>
      <NavLink activeClassName="active" to="/users">        Users
      </NavLink>
      <NavLink activeClassName="active" to="/contact">        Contact
      </NavLink>
    </nav>
  );
}
export default Header;

Next, we need to add a footer related code to footer.js file.

footer.js
import React from "react";

function Footer() {
  return (
    <div>
      <h1>This is footer</h1>
    </div>
  );
}

export default Footer;

We successfully created two components, now we will add a header, footer components to index.js file, so that we can see the both components on every page we navigate.

index.js
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import { Route, BrowserRouter as Router, Switch } from "react-router-dom";
import App from "./App";
import Users from "./users";
import Contact from "./contact";
import Notfound from "./notfound";
import Header from "./header";import Footer from "./footer";
const routing = (
  <Router>
    <div>
      <Header />      <hr />
      <Switch>
        <Route exact path="/" component={App} />
        <Route path="/users" component={Users} />
        <Route path="/contact" component={Contact} />
        <Route component={Notfound} />
      </Switch>
      <Footer />    </div>
  </Router>
);

ReactDOM.render(routing, document.getElementById("root"));

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2021 (Beginner + Advanced)
JavaScript - The Complete Guide 2021 (Beginner + Advanced)
64,026 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
152,857 students enrolled
48.5 hours of video content
View Course