avatarAurora

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3948

Abstract

lass="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>; <span class="hljs-keyword">import</span> <span class="hljs-title class_">Navbar</span> <span class="hljs-keyword">from</span> <span class="hljs-string">'./Navbar'</span>;

<span class="hljs-keyword">const</span> <span class="hljs-title function_">App</span> = (<span class="hljs-params"></span>) => { <span class="hljs-keyword">return</span> ( <span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>></span> <span class="hljs-tag"><<span class="hljs-name">Navbar</span> /></span> {/* Add your other components here */} <span class="hljs-tag"></<span class="hljs-name">div</span>></span></span> ); };

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-title class_">App</span>;</pre></div><p id="a56a">Finally, add some CSS code to style the navigation bar. Open the <code>src/App.css</code> file and add the following code:</p><div id="4326"><pre><span class="hljs-selector-tag">nav</span> { <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#333</span>; <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>; }

<span class="hljs-selector-tag">nav</span> <span class="hljs-selector-tag">ul</span> { <span class="hljs-attribute">display</span>: flex; <span class="hljs-attribute">justify-content</span>: space-around; <span class="hljs-attribute">list-style-type</span>: none; <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>; }

<span class="hljs-selector-tag">nav</span> <span class="hljs-selector-tag">li</span> <span class="hljs-selector-tag">a</span> { <span class="hljs-attribute">color</span>: <span class="hljs-number">#fff</span>; <span class="hljs-attribute">text-decoration</span>: none; }

<span class="hljs-selector-tag">nav</span> <span class="hljs-selector-tag">li</span> <span class="hljs-selector-tag">a</span><span class="hljs-selector-pseudo">:hover</span> { <span class="hljs-attribute">color</span>: <span class="hljs-number">#ccc</span>; }</pre></div><p id="4eaa">This CSS code styles the navigation bar to create a visually appealing layout.</p><p id="eb2b">To make the links work, install the <code>react-router-dom</code> package by running the following command in your terminal:</p><div id="91b3"><pre>npm install react-router-dom</pre></div><p id="ddc2">Or,</p><div id="ecd4"><pre>yarn add react-router-dom</pre></div><p id="2255">Then, update the <code>App.js</code> file to include the necessary components from <code>react-router-dom</code>:</p><div id="8838"><pre><span class="hljs-keyword">import</span> <span class="hljs-title class_">React</span> <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>; <span class="hljs-keyword">import</span> { <span class="hljs-title class_">BrowserRouter</span> <span class="hljs-keyword">as</span> <span class="hljs-title class_">Router</span>, <span class="hljs-title class_">Route</span>, <span class="hljs-title class_">Routes</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>; <span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>; <span class="hljs-keyword">import</span> <span class="hljs-title class_">Navbar</span> <span class="hljs-keyword">from</span> <span class="hljs-string">'./Navbar'</span>;

<span class="hljs-keyword">const</span> <span class="hljs-title function_">App</span> = (<span class="hljs-params"></span>) => { <span class="hljs-keyword">return</span> ( <span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">Router</span>></span> <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">cl

Options

assName</span>=<span class="hljs-string">"App"</span>></span> <span class="hljs-tag"><<span class="hljs-name">Navbar</span> /></span> <span class="hljs-tag"><<span class="hljs-name">Routes</span>></span> <span class="hljs-tag"><<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">exact</span>></span> {/* Add your Home component here /} <span class="hljs-tag"></<span class="hljs-name">Route</span>></span> <span class="hljs-tag"><<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span>></span> {/ Add your About component here /} <span class="hljs-tag"></<span class="hljs-name">Route</span>></span> <span class="hljs-tag"><<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/contact"</span>></span> {/ Add your Contact component here */} <span class="hljs-tag"></<span class="hljs-name">Route</span>></span> <span class="hljs-tag"></<span class="hljs-name">Routes</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span> <span class="hljs-tag"></<span class="hljs-name">Router</span>></span></span> ); };

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-title class_">App</span>;</pre></div><p id="2e24">Now, when you run the project by executing <code>npm start</code> or <code>yarn start</code> in the terminal, you can navigate between the Home, About, and Contact pages using the navigation bar.</p><p id="d8cc">This solution demonstrates how to create a navigation bar in ReactJS using the <code>react-router-dom</code> package. You can further customize the styling and functionality of the navigation bar as needed.</p><blockquote id="52ea"><p><b>Disclaimer: This is a very basic project for beginners to experience and learn their wayabouts in React, probably first-time encountering React? If you aren’t an absolute beginner, I don’t think this step-by-step introduction project is suitable for you.</b></p></blockquote><h2 id="911c">If you enjoyed learning the basics of everything or just React, I have created a list that I will be writing and adding them to →</h2><p id="f954"><a href="https://forelsketaura.medium.com/list/programming-coding-3d4395462530"><b><i>https://forelsketaura.medium.com/list/programming-coding-3d4395462530</i></b></a></p><figure id="435a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*ER86MyPF_HEg8Vzz"><figcaption>Photo by <a href="https://unsplash.com/@mahkeo?utm_source=medium&amp;utm_medium=referral">Khamkéo Vilaysing</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h1 id="7c69">PlainEnglish.io 🚀</h1><p id="43e6"><i>Thank you for being a part of the In Plain English community! Before you go:</i></p><ul><li><i>Be sure to <b>clap</b> and <b>follow</b> the writer</i><b></b></li><li><i>Learn how you can also <a href="https://plainenglish.io/blog/how-to-write-for-in-plain-english"><b>write for In Plain English</b></a></i></li><li><i>Follow us: <a href="https://twitter.com/inPlainEngHQ"><b>X</b></a><b> | <a href="https://www.linkedin.com/company/inplainenglish/">LinkedIn</a> | <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw">YouTube</a> | <a href="https://discord.gg/in-plain-english-709094664682340443">Discord</a> | <a href="https://newsletter.plainenglish.io/">Newsletter</a></b></i></li><li><i>Visit our other platforms: <a href="https://stackademic.com/"><b>Stackademic</b></a><b> | <a href="https://cofeed.app/">CoFeed</a> | <a href="https://venturemagazine.net/">Venture</a></b></i></li></ul></article></body>

Beginners Guide: How to Code a Navigation Bar in ReactJS

Handling Navigation with React Router in Your Nav Component

Photo by aisvri on Unsplash

As you might have already got an idea of what we will be building today, yes, we are going to cover the fundamentals of ReactJS for absolute beginners, which is a very simple and basic navigation bar. So, without further ado, let’s get started!

You might want to create an empty folder on your desktop and name it any name you wish. Open this folder in VSCode, then set up a new ReactJS project by running the following command in your terminal:

npx create-react-app navigationbar-app

Navigate to the project directory:

cd navigationbar-app

Create a new file named Navbar.js in the src directory and add the following code:

import React from 'react';
import { Link } from 'react-router-dom';

const Navbar = () => {
 return (
    <nav>
      <ul>
        <li>
          <Link to="/">Home</Link>
        </li>
        <li>
          <Link to="/about">About</Link>
        </li>
        <li>
          <Link to="/contact">Contact</Link>
        </li>
      </ul>
    </nav>
 );
};

export default Navbar;

This code defines a navigation bar component with three links: Home, About, and Contact.

Next, update the App.js file to include the Navbar component:

import React from 'react';
import './App.css';
import Navbar from './Navbar';

const App = () => {
 return (
    <div className="App">
      <Navbar />
      {/* Add your other components here */}
    </div>
 );
};

export default App;

Finally, add some CSS code to style the navigation bar. Open the src/App.css file and add the following code:

nav {
 background-color: #333;
 padding: 10px;
}

nav ul {
 display: flex;
 justify-content: space-around;
 list-style-type: none;
 padding: 0;
}

nav li a {
 color: #fff;
 text-decoration: none;
}

nav li a:hover {
 color: #ccc;
}

This CSS code styles the navigation bar to create a visually appealing layout.

To make the links work, install the react-router-dom package by running the following command in your terminal:

npm install react-router-dom

Or,

yarn add react-router-dom

Then, update the App.js file to include the necessary components from react-router-dom:

import React from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import './App.css';
import Navbar from './Navbar';

const App = () => {
 return (
    <Router>
      <div className="App">
        <Navbar />
        <Routes>
          <Route path="/" exact>
            {/* Add your Home component here */}
          </Route>
          <Route path="/about">
            {/* Add your About component here */}
          </Route>
          <Route path="/contact">
            {/* Add your Contact component here */}
          </Route>
        </Routes>
      </div>
    </Router>
 );
};

export default App;

Now, when you run the project by executing npm start or yarn start in the terminal, you can navigate between the Home, About, and Contact pages using the navigation bar.

This solution demonstrates how to create a navigation bar in ReactJS using the react-router-dom package. You can further customize the styling and functionality of the navigation bar as needed.

Disclaimer: This is a very basic project for beginners to experience and learn their wayabouts in React, probably first-time encountering React? If you aren’t an absolute beginner, I don’t think this step-by-step introduction project is suitable for you.

If you enjoyed learning the basics of everything or just React, I have created a list that I will be writing and adding them to →

https://forelsketaura.medium.com/list/programming-coding-3d4395462530

Photo by Khamkéo Vilaysing on Unsplash

PlainEnglish.io 🚀

Thank you for being a part of the In Plain English community! Before you go:

Coding
Programming
Software Development
Web Development
JavaScript
Recommended from ReadMedium