Suppose I have my "/" page and another page "/Blog". I want to go from /Blog to /#Blogs, which is a route containing the specific element ID. I am trying to do this using the Link component.
here is the code snippet for my Link component:
<Link
href={displayAll ? "/#Blogs" : "/Blog"}
scroll={false}
{displayAll ? "Back Home" : "Read more"}
</Link>
P.s, I successfully navigate to localhost:3000/#Blogs, but I am redirected to the top of the page, rather than the element with id="Blogs"
2
Answers
you can simply use:
but be careful, its a little more complicated than that. when you navigate between pages, NextJS default behaviour is consider it server component, and if you wrap your page in Suspend component with a fallback, this wont work.
in that case, you have to do it manually, i.e when page loaded, push it to your desired element with UseEffect hook.
The issue arises because Next.js’s
Link
component doesn’t automatically handle scrolling to an element with a specific ID when navigating between pages. While you’re successfully navigating to/#Blogs
, Next.js doesn’t scroll to the element withid="Blogs"
because it only works on the same page by default.To fix this issue, you need to manually handle the scrolling. You can achieve this by listening to the
router
events and using JavaScript to scroll to the desired element. Here’s how you can do it:Updated Code Snippet
Explanation:
useEffect
androuter.events
:routeChangeComplete
events to detect when navigation finishes.#Blogs
), extract the ID and find the corresponding DOM element.scrollIntoView
:id="Blogs"
.scroll={false}
:Link
component, allowing custom behavior instead.Notes:
id="Blogs"
to your target element in the/
page for this to work.Let me know if you encounter any issues!