Using a JavaScript command, connect a function from one file to another file

I attempted to import a function because I want to click on <il> servies</il> and scroll to the services section on the home page. However, I simply want to click on the li element in the navbar and scroll to the service section on the home page. Both pages are different which is why my module is not working on NEXT.JS

**Images of code ** navbar code image services page code image

**Facing ERROR *

Import trace for requested module:
./components/Navbar.js
wait  - compiling...
error - ./components/Navbar.js
Error:
  x Expected ',', got ':'
    ,-[D:\Next.js projects\help-desk\components\Navbar.js:8:1]
  8 |   const ref  = useRef();
  9 |   const scrollToSection = (elementRef) =>{
 10 |     window.scrollTo(
 11 |       top:elementRef.current.offsetTop,
    :          ^
 12 |       behavior: 'smooth'
 13 |     )
 13 |   }
    `----

Caused by:
    Syntax Error

Import trace for requested module:
./components/Navbar.js

navbar.js Code:

import React from 'react'
import Image from 'next/image'
import Link from 'next/link'
import { useRef } from 'react'
import { AiOutlineSearch } from 'react-icons/ai';
AiOutlineSearch
const Navbar = () => {
  const ref  = useRef();
  const scrollToSection = (elementRef) =>{
    window.scrollTo(
      top:elementRef.current.offsetTop,
      behavior: 'smooth'
    )
  }
  return (
    <>
    <div className='flex flex-col md:flex-row md:justify-start justify-center items-center py-1 drop-shadow-xl bg-slate-100'>
    <div className='logo mx-9 cursor-pointer'>
      <Image width={70} height={70} src="/navhome.png" alt=" " />
    </div>
    <div className="nav">
        <ul className='flex items-center space-x-6 font-bold md:text-md'>
          <Link legacyBehavior href={"/"}><a><li>Home</li></a></Link>
          <Link legacyBehavior href={"/about"}><a><li>About</li></a></Link>
          {/* <Link legacyBehavior href={"/servies"}><a><li>Services</li></a></Link> */}
          <li onClick={()=> scrollToSection(ref) href="#section"}>Services</li>
          <Link legacyBehavior href={"/contact"}><a><li>Contact</li></a></Link> 
        </ul>
      </div>
      <div className="search absolute right-0 top-6 mx-5">
       <AiOutlineSearch className='text-xl  md:text-3xl'/>
      </div>
    </div> 
    </> 
  )
}

export default Navbar;
export { scrollToSection };

index.js file:

import Image from 'next/image'
import Head from 'next/head'
import Link from 'next/link'
import  {scrollToSection}  from '../components/Navbar.js'
import Servies from '../pages/servies'

export default function Home() {
  return (
    <div>
      <Head>
        <title>HelpDesk.com - we're here to serve you </title>
        <meta name="description" content="HelpDesk.com - we're here to serve you" />
        <link rel="icon" href="/favicon.ico" />
      </Head>
      <div>
        <img src="/home.jpg" alt="" />
      </div>
      <section ref={ref} className='scrollService' id='sectionid' >
        <Servies />
        </section>
      <br />
    </div>
    )
}

Answer №1

If you want to achieve the desired outcome, consider using the id attribute of your section as the target for a link.

<section id="section"/>

In order to do this in Next.js:

<Link href="#section"/>

For React:

<Link to="#section"/>

And in plain HTML:

<a href="#section">Scroll to section</a> 

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

Tips for preparing HTML content with Jquery's "ON" method?

My jQuery Accordion is functioning properly, but I'm encountering issues when trying to load the accordion content dynamically from a database or JSON onto the page. The problem arises because the DOM doesn't have information about the newly inje ...

Unlock the secrets of creating an interactive chat room effortlessly by harnessing the power of J

In search of implementing a unique chat room using PHP and JavaScript (Jquery) offering group chat as well as private chat functionalities. The challenge lies in finding a way to continuously update the interface seamlessly, while also displaying 'X ...

Automatically submitting forms without having to refresh the page

I have been searching for answers online, but none of them seem to help me. Additionally, I am struggling to grasp the concept of Ajax. I need everything to happen on a single page without any refreshing until the entire form is submitted. <form id=" ...

I am looking to transfer information to a different screen using FlatList in React Native

Hey everyone, I'm new to React Native and I'm trying to create a news app with posts. I'm having trouble figuring out how to pass data in a flatlist to another screen and open the screen with the post data. I'm using V6 navigation. Here ...

Tips for identifying MIME type errors in an Angular 9 application and receiving alerts

While working on my Angular app, I encountered the MIME type error Failed to load module script: The server responded with a non-javascript mime type of text/html. Fortunately, I was able to resolve it. Now, I'm stuck trying to figure out how to rece ...

Refresh a specific portion of an HTML template following a successful AJAX request

I am facing a challenge in implementing a new feature and I'm unsure of the best approach to take. In my project, I utilize Django for backend logic and templating, as well as the Google Closure JavaScript library for frontend development. Here is th ...

How can I add a Subresource Integrity Check to a Next.js project? Is it possible in Next.js 12, 13, or 14?

Implementing the experimental sri module. experimental: { sri: { algorithm: 'sha256' } }, The subresource integrity manifest files are generated, however, the integrity attribute is not injected into the build. We were anticipat ...

What is the reason for DialogContent displaying a scroll bar instead of expanding further when nested Grids with spacing are included?

My current project involves working on a form displayed in a dialog window. To adjust the layout of the form's fields, I utilize multiple Grid elements nested within another Grid. However, I've encountered an issue where adding any spacing to the ...

Error: The module you are trying to import from the package is not found. Please check the package path and make sure that

I encountered an issue when trying to compile a code in Reactjs. As a beginner in Reactjs, I'm struggling with this. Module not found: Error: Package path ./cjs/react.development is not exported from package /Users/mansi/letsgrowmore/to-do-list/my-rea ...

What are the steps for scheduling asynchronous tasks using Promises in Node.js?

As a beginner in Javascript, I am struggling to understand how to execute my functions sequentially. My goal is to use Promises to accomplish this task. I am currently following a tutorial on creating a chat bot for Facebook Messenger. Essentially, I want ...

Guide on utilizing getelementsbytagname for retrieving LI values

As I attempt to locate a specific product on amazon.com, my goal is to extract the ASIN for each item. This particular code snippet runs through various search options and retrieves the price of each product from Amazon one by one. However, in addition to ...

Error: The specified module is missing an export that was requested

Hello, I encountered an error in my console that reads: "Uncaught SyntaxError: The requested module './components/tileHeader/controller.js' does not provide an export named 'tileHeader'". Below is the code snippet causing the issue: co ...

When accessing nested objects in Props in Vue.js, an error may occur if the property of null/undefined

When I make an axios call, I receive an object which I pass to a child component. However, when I attempt to loop through the object in the child component to access a property of another nested object, I encounter an issue where the property is showing as ...

The ultimate guide to storing and retrieving images in a MySQL database with the help of Spring REST and ReactJS

Looking for assistance in developing an application that utilizes React and Spring REST to save and retrieve images from a MySQL database ...

What steps can I take to resolve the issue of the Error value not being iterable?

I encountered an issue in my table where I get the error message value is not iterable when there is no value to iterate through. How can I handle this error in my code? Snippet of Code: if (null != data && data) { data = data.map((item) => ...

Tips for utilizing node.io for HTML parsing with node.js?

Struggling to utilize node.io with node.js for parsing an HTML page stored as a string in a variable. Encountering difficulties passing the HTML string as an argument to my node.io job. Snippet from my node file nodeiotest.js: var nodeIOJob = requi ...

Error in Next.js with React Aria: ID prop does not match

While experimenting with React Aria in Next.js, I came across an error message that reads: Warning: Prop id did not match. Server: "react-aria-3" Client: "react-aria-10" Even after wrapping _app.js with the SSRProvider component, the i ...

Issue with displaying PDF files on Google Chrome due to a software glitch

Recently, I encountered a puzzling issue on Google Chrome. I am not sure if the error lies with Google or within my code. When I set the div as display: none; and then show it again, the PDF view only shows a grey background. However, if I zoom in or out, ...

Rails 3.2 - form mistakenly submitted repeatedly

I am working on a project involving the Box model, which includes many box_videos. I have created an edit form to allow users to add box_videos to the box after it has been created: <%= form_tag "/box_videos", { method: :post, id: "new_box_videos", rem ...

What is the process for making an Ajax request in Rails?

I'm attempting to send a variable through jQuery using the POST method, saving it in a controller, and then utilizing that same variable in Rails HTML to query a table. It seems like the variable isn't being passed to the controller. jQuery: v ...