Next.js Discord

Discord Forum

Help to redirect to a specific section

Unanswered
Northeast Congo Lion posted this in #help-forum
Open in Discord
Northeast Congo LionOP
i want the user to get redirected to another route but in a specific section holding the id banner
redirect("/success#banner");
but its not working. what's wrong?

26 Replies

Northeast Congo LionOP
here is my redirect function
but i get this
White-winged Scoter
@Northeast Congo Lion First before I assume something else, do you actually have an element with id={"banner"?
and when i manualy use /success#banner
it works
White-winged Scoter
Then It's the redirect call.
@Northeast Congo Lion here is my redirect function
Northeast Congo LionOP
btw, that ",sections" was a mistake i just tried smthing
@White-winged Scoter Then It's the redirect call.
Northeast Congo LionOP
i see
White-winged Scoter
It's probably that redirect isn't able to pass query, because it's not trying to fetch, that would be my guess.
White-winged Scoter
Is this on the server?
Northeast Congo LionOP
no
i mean
yes
sorry
White-winged Scoter
Oh, well if it was on the client you could do:
export default component() {
 const router = useRouter()
 router.push("/success#banner")
 return ([...])
}
@Northeast Congo Lion Is your success page on the client?
Northeast Congo LionOP
yes
it is
White-winged Scoter
Oh that makes it easy
Just have this inside your page
useEffect(() => {
 var banner = /* Define your element */
 banner.scrollIntoView({behavior: "smooth"})
},[])
So that will automatically scroll to the banner whenever the page loads
just make sure its obviously in the top level of your page, and ofc import useEffect
Northeast Congo LionOP
@White-winged Scoter thank you so much!
White-winged Scoter
No worries, good luck on your project.