Next.js Discord

Discord Forum

How do you redirect from onClick handler?

Unanswered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
I am using Listbox for a few items and would like each item to be redirected to another URL with the help of href. The problem is that I cannot use href in ListboxItem. If I add anything else other than a ListboxItem, I get an error. I was adding Link from next/link and it was returning an error. I suppose that I would have to use the onClick event to redirect the user to the URL of my choosing. Which library is most suitable for this or how else would I approach this problem? Middleware perhaps?

This is the Listbox component I'm using.
https://nextui.org/docs/components/listbox

1 Reply

Transvaal lionOP
I imported useRouter and had a handler on each onClick, such as:
onClick={() => router.push(`/#${key}`)}

key being where I wanted the redirection to go!