Next.js Discord

Discord Forum

Redirect doesn't redirect

Answered
Gecco posted this in #help-forum
Open in Discord
Hi. I have a select tag and onChange={({ target }) => redirect(/?poem=${target.value})} attached to it which doesnt redirect me when clicked.
Answered by West African Lion
redirect() is for server, useRouter() is for client

import { useRouter } from 'next/router'

const router = useRouter();

  onChange=(() => router.pushState('{YOUR_URL}'));
View full answer

4 Replies

West African Lion
redirect() is for server, useRouter() is for client

import { useRouter } from 'next/router'

const router = useRouter();

  onChange=(() => router.pushState('{YOUR_URL}'));
Answer
West African Lion
redirect() returns an http response with a redirect status code along with a Location header, useRouter will use JS on the client side