Creating a navigation bar in React using react-scroll

Is anyone able to assist me with resolving the issue I am facing similar to what was discussed in React bootstrap navbar collapse not working? The problem is that although everything seems to be functioning properly, the navbar does not collapse when clicking on any nav item in responsive mode.

import React from 'react'
import Logo from "../img/logo_large.png"
// Imports for Font Awesome in React
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faBars } from "@fortawesome/free-solid-svg-icons"
// Installing React-scroll
import { Link} from "react-scroll"

//Navbar Component
const Navbar = () => {
    return (
        //This section allows customization of the navigation bar (e.g., dark mode)
        <nav className="navbar navbar-expand-lg navbar-light bg-dark fixed-top">
            <div className="container">
                {/*Logo variable created above*/}
                <Link smooth={true} eventKey="2" to="home" className="navbar-brand" href="#"><img className="Logo" src={Logo} /> </Link>
                <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                    <FontAwesomeIcon icon={faBars} style={{ color: "#fff" }} />
                </button>

                <div className="collapse navbar-collapse" id="navbarSupportedContent" data-toggle="collapse" data-target=".navbar-collapse.show">
                    {/*ml-auto --> to align content to the right*/}
                    <ul className="navbar-nav ml-auto">
                        <li className="nav-item active">
                            <Link smooth={true} eventKey="2" to="about" offset={-70} className="nav-link" href="#">About Me<span className="sr-only">(current)</span></Link>
                        </li>
                        <li className="nav-item">
                            <Link smooth={true} eventKey="2" to="services" offset={-70} className="nav-link" href="#">Services</Link>
                        </li>
                        <li className="nav-item">
                            <Link smooth={true} eventKey="2" to="experiences" offset={-70} className="nav-link" href="#">Experiences</Link>
                        </li>
                        <li className="nav-item">
                            <Link smooth={true} eventKey="2" to="abilities" offset={-70} className="nav-link" href="#">Abilities</Link>
                        </li>
                        <li className="nav-item">
                            <Link smooth={true} eventKey="2" to="blog" offset={-70} className="nav-link" href="#">Blog</Link>
                        </li>
                        <li className="nav-item">
                            <Link smooth={true} eventKey="2" to="freetime" offset={-70} className="nav-link" href="#">Freetime</Link>
                        </li>
                        <li className="nav-item">
                            <Link smooth={true} eventKey="2" to="contact" offset={-70} className="nav-link" href="#">Contact</Link>
                        </li>
                    </ul>
                </div>
            </div>
        </nav >
    )
}

export default Navbar

Answer №1

To activate the toggle feature, React Bootstrap is necessary. Refer to this link for a demo of how it works.

If you prefer not to utilize React Bootstrap, simply employ the onClick event handler for toggling the navigation bar manually.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporate fresh Google sites into different web pages using iFrame integration

Wishing you a fantastic day! I am currently working on embedding a brand new Google site into another webpage. I attempted to use an iframe for this purpose, but unfortunately it did not work as expected. Here is the code snippet: <iframe width="1280 ...

Managing the display of numerous ngFor components

If you're interested in learning more about the features I will include, here's a brief overview. I plan to have a project section with cards displayed for each project, all populated from a JSON file. When users click on a card on the website, a ...

The request to generate a worker from 'blob:<URL>' was denied due to infringement of the Content Security Policy rule "child-src 'self' *.website.com."

I am currently developing a Chrome extension (MV3) that involves loading libraries which use workers. I have added the worker files (.js and .wasm) to the resource folder in order to load them "locally" into the Chrome extension, and they are also included ...

What could be causing my carousel layout to display vertically instead of horizontally when using flex?

I currently have a carousel set up to display store products. However, I am encountering an issue where the images are rendering vertically on the screen instead of overlapping each other as they should in a carousel format. You can see the code in action ...

Error message indicating a prop type warning occurred in an inactive component

I received a warning message for a page that I am not currently visiting (I'm on the https://example.com/this-page). Warning: Failed prop type: The prop `query` is marked as required in `OtherPage`, but its value is `undefined`. "prop-types&quo ...

Tips for adjusting the wrapper css rule of a Tabs component to left-align text in a vertical tab within Material-UI

Typically, when text is placed inside the wrapper, it is aligned in the center. Is there a way to adjust the wrapper rule in <span class="MuiTab-wrapper">Item One</span> so that the tab text is aligned to the left (similar to what w ...

Why does my element appear to be a different color than expected?

I've developed a sleek wind map (check out for reference). Using a weighted interpolation every 10ms, I generate uVector and vVector data to calculate wind speed. Based on this speed, each point is assigned a specific color as outlined below. if (we ...

What is the best way to turn off default CSS styling in KendoUI?

I am facing an issue in my application where I am using global CSS definitions for "INPUT", "SELECT", and other elements. The problem arises when I try to incorporate KendoUI widgets, as they override my default CSS styles. For instance, my CSS code looks ...

Modify the color of the active class in Bootstrap

I am trying to modify the color of the active class in my HTML code as I create a nav sidebar. Here is the snippet from my code: <div class="col-sm-2"> <ul class="nav nav-pills nav-stacked nav-static"> <!--stacked for vertic ...

"Exploring the world of CSS3: Arrow shapes and animated effects

Looking at the image provided, my task is to create and animate it. I was considering using CSS3 border-radius instead of an actual image. Below are the HTML and CSS code that I have so far. The animation I envision involves the arrow gradually appearing ...

Mern, Issue with Removing Comments

Having trouble deleting comments within a single post? I keep encountering a 404 Error and I'm not sure why the post is not being found. I really need some assistance in figuring out what's causing this issue and why I'm unable to delete a c ...

Steps for launching a hyperlink in a browser on Vue Native

I'm having trouble finding information on how to properly use the Linking component in react-native. The resources available are limited, and I'm not sure if I am implementing it correctly... Below is what I have in my template: <nb-button :o ...

Creating a website without access to the internet

When I'm working on my laptop in locations without internet access, I need to build a website. How can I assess the progress of my pages without an active browser? ...

Neglecting to review the CSS - embracing ejs layouts in Express

I am encountering an issue with the express ejs layouts where only the mainPage is able to read the CSS, while the other pages are unable to do so (even though the HTML reads it). Additionally, if I want to use another layout such as "layout2.ejs", what s ...

Is there a way to modify the characteristics of a specific dependency?

I'm currently in the process of learning how to use the npm library, and came across this carousel. I successfully integrated it into my project, but now I find myself unsure about modifying its attributes. For reference, here's the documentation ...

Is it possible to use JavaScript to forcefully transition a CSS keyframe animation to its end state?

One dilemma I am facing involves CSS keyframe animations triggered by scroll behavior. When users scroll too quickly, I would like JavaScript to help send some of the animations to their 'finished/final' state, especially since the animations bui ...

Challenges with the efficiency of the Material UI Datagrid

I am currently using MUI Datagrid to display my records, but I am experiencing delays with my modal and drawer components. Even after attempting to optimize with useMemo for my columns, I have not been able to achieve satisfactory performance. https://i.st ...

There seems to be an issue with the location.href function in the server-side code of

I'm encountering an issue in the code below where I am attempting to redirect to the login page, but it seems to not be functioning as expected. app.get('/current-pass', (req, res) => { res.sendFile(path.join(staticPath, "currentPass ...

What is the best way to create a div that resembles a dotted line?

I have a bar chart type created using different div elements. CSS: .outer, .inner, .target { height: 14px; margin-bottom: 5px; } .outer { background-color: #cccccc; width: 200px; margin: 0 auto; position: relat ...

Updating state on nested arrays in React components involves using the `

UPDATE** Currently, the structure of my React state is as follows: this.state = { data: { groups: [[]] }, preview: [[]] } Each element in groups is an array that contains files. I have a file change event handler that currently looks ...