vercel / next.js

The React Framework
https://nextjs.org
MIT License
124.99k stars 26.7k forks source link

`redirect()` function not redirecting in client components #59163

Open pedro757 opened 9 months ago

pedro757 commented 9 months ago

Link to the code that reproduces this issue

https://github.com/pedro757/redirect-issue-client-components

To Reproduce

  1. Click the button

Current vs. Expected behavior

Currently it's not redirecting to the otherpage I expect to actually redirect to the otherpage

Verify canary release

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Mon Nov 20 12:57:36 UTC 2023
Binaries:
  Node: 20.5.1
  npm: 10.2.1
  Yarn: 1.22.19
  pnpm: 8.10.3
Relevant Packages:
  next: 14.0.4-canary.32
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

App Router, Routing (next/router, next/navigation, next/link)

Additional context

I have used redirect in server actions and it seems to be working good, the problem seems to be on client components

Fredkiss3 commented 9 months ago

redirect is function that can only be used in server components, you should instead use router.push() on the client.

pedro757 commented 9 months ago

redirect is function that can only be used in server components, you should instead use router.push() on the client.

In the first paragrapth the docs say we can use it in client components, however I believe the explanation to this problem is this

If that's the case, I wonder what an use case for redirect in client components would be?

jvdhr commented 2 months ago

redirect in server components/actions doesn't seem to work if it's being called in a trycatch block.

ceyhun-yildiz commented 2 months ago

redirect in server components is not working for me in try/catch block

Screenshot 2024-07-16 at 11 56 44
yassin-mokni commented 1 month ago

To perform client-side navigation (redirect) in a client component, you should use the router.push() method from Next.js's useRouter hook. , replace the redirect()call with router.push().