Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

796
Views
Evitar ir a la página anterior en el botón Atrás del navegador

Tengo un proyecto Next.js / React.js en el que estoy trabajando y estoy tratando de redirigir a una página determinada cuando se hace clic en el botón Atrás del navegador. Mi enfoque es el siguiente

 useEffect(() => { Router?.beforePopState(({ as }) => { if (as !== router.asPath) { handleRouteChange({ toHref: getRoutingPath('HOME') }, Router.push); } return true; }); return () => { Router?.beforePopState(() => true); }; }, [dispatch, router]);

Y este código funciona. Cuando hago clic en el botón Atrás, me lleva a la página de inicio según lo previsto.

Pero el problema es que cuando hago clic en el botón Atrás, me lleva a la página anterior por un segundo y luego me redirige a la página de inicio. ¿Cómo puedo evitar ir a la página anterior?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede devolver false desde la devolución de llamada beforePopState para deshabilitar Next.js que maneja el enrutamiento automáticamente y manejarlo usted mismo.

De la documentación de router.beforePopState :

Si cb devuelve false , el enrutador Next.js no manejará popstate y usted será responsable de manejarlo en ese caso. Consulte Deshabilitar el enrutamiento del sistema de archivos .

 useEffect(() => { router.beforePopState(({ as }) => { if (as !== router.asPath) { handleRouteChange({ toHref: getRoutingPath('HOME') }, Router.push); return false; } return true; }); return () => { router.beforePopState(() => true); }; }, [router]);
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!