What is the best way to achieve a smooth scroll effect for a navbar item using this npm

I'm almost done with my portfolio but I'm struggling to figure out how to change the scrolling behavior of the "Contact me" nav item using the npm react-responsive-navbar-overlay.

Check out my Portfolio here:

Here is the code for my navigation:

import "../Css/index.css";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import { Navbar } from "react-responsive-navbar-overlay";
import Home from "./Home";
import Contact from "./Contact";
import Letter from "../Assets/img/letra-mayus-azul-25medium.png";

function Nav() {

  function scrollToTestDiv(){
    const divElement = document.getElementById('contact');
    divElement.scrollIntoView({ behavior: 'smooth' });
  }

  return (
    <BrowserRouter>
      <Navbar fontColor="#000" backgroundColor="#fff" brand={<img onClick={scrollToTestDiv} className="logo" src={Letter} />} links={[{text: "Contact me", link: "#contact"}]} />
      <Routes>
        <Route exact path="/" component={Home} />
        <Route path="/#contact" component={Contact} />
      </Routes>
    </BrowserRouter>
  );
}

export default Nav;

It seems that when you click on the logo it scrolls smoothly, but the nav item doesn't. How can I fix this?

Answer №1

Upon clicking on the "contact" button, the URL is modified to lead you to . Make sure to explore the "contact" component. In addition, consider utilizing react-scroll instead of react-responsive-navbar-overlay. This choice is widely recognized in various projects and online forums.

Here is a demonstration of how it functions and a related question that may interest you.

react-scroll | How to scroll to a specific targeted component when clicking on Navbar Link

I trust that my response has been helpful for you.

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

Failure to trigger a follow-up function in webSQL transaction's success callback

Review the code below. I have called setQuestion() within the successCallBack of db.transaction but am encountering an error: Uncaught TypeError: this.setQuestions is not a function. Can you spot any issues in my code? game.module( "game.scenes.scene" ) ...

Refresh the screen with modifications from the Model using Ajax Post through a javascript function

I am using Ajax to update the model value in my application and I need help showing this new value in the view. Below is the code snippet where I call a method called GetText to update the model value. How can I display the updated model value in the HTML ...

What is the best way to align a button directly beside an input field using Bootstrap's columns and grid system?

Is it possible to place a button right next to an input box while utilizing the bootstrap columns/grid system? The Label is set to 2, EditorFor div to 4, and the input div to 6. When I reduce the EditorFor div to 3, it moves the input/button div closer tog ...

Would it be practical to use a 1kb .png image on repeat as a background image?

I recently created a PNG image from the website . This image is only 1kb in size and I am currently using it as the background-image on the body tag of my webpage. Now, I am contemplating using another image that is significantly larger at 500kb (with dim ...

Experiencing problems with integrating Slim framework and AngularJS, such as encountering a 404 error

Although this may seem like a repeat question, I am encountering an issue with using AngularJS with Slim Framework web services. I have set up a webservice to retrieve a student record with a URL structure like: http://www.slim.local/api/getstudent/1 ...

Using the spread operator to modify an array containing objects

I am facing a challenge with updating specific properties of an object within an array. I have an array of objects and I need to update only certain properties of a single object in that array. Here is the code snippet I tried: setRequiredFields(prevRequir ...

Is there a way to create a reusable HTTP response handler for Node.js applications?

As I work on creating a rest api for a node application, I often find myself repeating the same code structure: function(req, res, next) { databaseCall() .then( (results) => { if (results != null) { res.status(200).send(results); } el ...

adjust margin-top as user scrolls

I currently have a negative margin set on a div, but I am looking to gradually change this negative margin to 0 as the user scrolls down. From: margin:-150px 0 0 0; To: (on scroll) margin:0px 0 0 0; I believe this effect is similar to parallax scrolli ...

Detecting changes in arrays in Vue.js 2

Below is a simplified version of the code : <template> /* ---------------------------------------------- * Displaying a list of templates, @click to select the template /* ---------------------------------------------- <ul> ...

The directive in an ionicModal is executed first, even before the controller is fully loaded

I recently encountered a challenge with a $ionicModal containing a directive called <scrap-link-cards>, which accepts a two-way binding scope object. This directive is included in the $ionicModal template: <scrap-link-cards datasource=(updates.up ...

Switching between states using JavaScript

Here is a code snippet I'm working with: <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="C:\Users\rapandey\Documen ...

Retrieve nodes that are children of another node

Here is the code snippet that I am working with: <div id="page1-div" style="position:relative;width:1347px;height:1189px;"> <img width="1347" height="1189" src="target001.png" alt="bac ...

Utilizing MUI Drawer with React Router v6's useNavigate Function

As I dive into using stackblitz to incorporate MUI Drawer and react-router-dom for an upcoming application, I'm struggling with the implementation of the useNavigate hook. Any guidance on how to correct this would be greatly appreciated. Drawer.js co ...

Get connected to your favorite music on iTunes without the hassle of opening a new window by simply clicking on the

Is there a way to call a link to iTunes (itms://...) without opening a new window? I've tried using window.open but that just opens a new window. Also, $.get('itms://...'); doesn't seem to work for me. Are there any other options avail ...

Utilizing Supabase queries alongside React's Hot Toast Promise: A Comprehensive Guide

I'm currently working on an admin panel to manage my website. As part of the development, I am using Supabase for the Database and React Hot Toast for notifications. Recently, I attempted to implement Toast Promise using the following code: const add ...

Managing errors in NodeJS DELETE endpoint using findByIdAndDelete function

Seeking the most effective way to manage a catch error in the output or response. This route is responsible for deleting a game based on its ID router.delete("/delete/:id", (req, res) => { (async () => { try { let deleteGame = await ga ...

Issue with Firebase authentication API key (auth/invalid-api-key) encountered on the production website, yet functioning properly on the local

While my React App works fine locally, it encounters an error and fails to load on the live site. The specific error message displayed is as follows: Uncaught FirebaseError: Firebase: Error (auth/invalid-api-key). at Ut (main.d108a90a.js:2:403619) ...

Tips for building and implementing Angular URL Parameters for URLs in the form: "component/#/?id=..."

I am currently facing a situation where I have an application with an existing user base. I am looking to avoid disrupting their current links for a smoother transition. However, the previous links are in this format: (server)/viewer/#/?id=12. Please see t ...

Custom sparkline array

In working on my sparkline chart, I have the following code snippet: <div sparkline="" values="4,4,7,5,9,6,4" data-type="line" data-height="80" data-width="100%" data-line-width="2" data-line-color="#dddddd" data-spot-color="#bbbbbb" data-fill-c ...

"Troubleshooting: React Hook Form Still Causing Page Refresh Upon Submission - How to Fix

Struggling to create a form without page refresh on submit? Here's the code snippet I'm working with: 'use client' import * as React from 'react'; import {useForm} from "react-hook-form" import Box from '@mui/ma ...