I am trying to redirect the user from the error page in the app folder back to the main page.
I am building a weather , so in case the user inputs a wrong city he should be able to go back to the Home Screen. I tried using router but it didn’t work.here is the error page
"use client";
import Nav from "@/components/Nav";
import Wrapper from "@/components/Wrapper";
import { useRouter } from "next/navigation";
const Error = ({ error, reset }) => {
const router = useRouter();
const handleGoBack = (e) => {
e.preventDefault();
console.log("go back");
router.push(`/?city=london`);
};
return (
<div>
<form onSubmit={handleGoBack}>
<button>go back</button>
</form>
</div>
);
};
export default Error;
I also tried using next() but it just retry the fetch the wrong city.
2
Answers
Next 13 has a few helpers that they export from
next/navigation
, includingredirect
. If you want to redirect on the server, then:Then:
redirect('/?city=london')
however you need it in your page/action.