Author avatar

Gaurav Singhal

How to Create a Single Page React.js Page App with Different CSS for Each View

Gaurav Singhal

  • Aug 9, 2019
  • 5 Min read
  • 15 Views
  • Aug 9, 2019
  • 5 Min read
  • 15 Views
Web Development
React

Introduction

In this guide, we are going to see how to write different CSS for each page in React with react-router-dom using react-helmet.

There might be scenarios where you only need minimal CSS for a particular page. For example, let’s consider a login page. You don't require the stylesheet of the entire application, until and unless the user is authenticated. So, in that case, it would be unfair for the user to wait for the browser to download the CSS of the whole application, thus leading to poor user experience.

React Helmet (react-helmet)

Helmet is a reusable React component that manages all of your changes to the document head.

Usage

1
2
3
4
5
6
7
8
9
import {Helmet} from 'react-helmet';

// ...
<Helmet>
    <meta charSet="utf-8" />
    <title>My Title</title>
    <link rel="canonical" href="http://mysite.com/example" />
</Helmet>
// ...
jsx

The <Helmet /> component injects the plain HTML elements into the <head> tag of the application. It can contain meta, title, link, style, or any other valid HTML head tags.

Let's Begin with Our Code

First, let's create a "Pages" folder inside the src directory, that will contain all of our page components. We will have two pages, Login and Home, and use the <Helmet /> component in the Login Page.

index.js File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";

import Login from "./Pages/Login";
import Home from "./Pages/Home";

function App() {
  return (
    <Router>
      <div>
        <nav>
          <ul>
            <li>
              <Link to="/">Login</Link>
            </li>
            <li>
              <Link to="/home/">Home</Link>
            </li>
          </ul>
        </nav>

        <Route path="/" exact component={Login} />
        <Route path="/home/" component={Home} />
      </div>
    </Router>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
jsx

Pages/Home.js File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import React from "react";

import "../styles.css";

function Home() {
  return (
    <div className="App">
      <h1>Home Page</h1>
      <p>This is the Home Page</p>
    </div>
  );
}

export default Home;
jsx

Pages/Login.js File

In the Login page, we are going to add the <Helmet /> Component and inject our link tag for the login stylesheet into the head.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import React from "react";
import { Helmet } from "react-helmet";

function Login() {
  return (
    <div className="login container">
      <Helmet>
        <link rel="stylesheet" href="login.css" />
      </Helmet>
      <h1>Login</h1>
      <p>This is the login page</p>
    </div>
  );
}

export default Login;
jsx

Alternatively, we can also include the link tag, as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function Login() {
  return (
    <div className="login container">
      <Helmet link={ 
          [ 
            "rel": "stylesheet", 
            "href": "login.css" 
          ] 
        }/>
      <h1>Login</h1>
      <p>This is the login page</p>
    </div>
  );
}
jsx

styles.css file

1
2
3
4
5
6
7
8
.App {
  font-family: sans-serif;
  text-align: center;
}

h1 {
  color: #fc3;
}
css

login.css File

Add the login.css file inside the public folder.

1
2
3
4
5
6
7
h1 {
  color: #e09;
}

p {
  font-size: 24px;
}
css

Now when you switch from Login page to the Home page, you'll notice that the <h1> element in the Home page has a different color than the Login page and that the font family in the pages are different as well.

Other Alternative if You Don't Want to Use React Helmet

If for some reason, you don't require head tag management and don't want to bloat your app with an extra library, you can use the CSS in JS approach which is supported natively in React. So, in a page component, you can write something like as follows:

Pages/Login.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import React from "react";
import { Helmet } from "react-helmet";

function Login() {
  return (
    <>
      <div className="login container">
        <h1>Login</h1>
        <p>This is the login page</p>
      </div>
      <style
        dangerouslySetInnerHTML={{
            __html: `
            h1 {
                color: #e09;
            }

            p {
                font-size: 24px;
            }          `
        }}
      />
    </>
  );
}

export default Login;
jsx

Conclusion

React Helmet is a good library for your HTML head tag management. It can update the meta tags, add favicon, and update the title for each page. That's it from this guide, I hope you learned something new today. Please follow my other guides on React All You Need to Know About Axios Formspree AJAX with ES6 Submit Form in React without JQuery AJAX

0