I am running into an issue with useRouter
and useEffect
where I want to re-direct the user if a certain condition is not met.
On page 1 you select an amount
which is passed into a context and is retrieved on page 2.
On page 2 I have this condition:
import { useEffect, useContext } from "react";
import { useRouter } from "next/router";
import AppContext from "../context/AppContext";
const PageTwo = () => {
const { amount } = useContext(AppContext);
const router = useRouter();
useEffect(() => {
if (!amount || amount == null) {
router.push("/page-1"); // redirect if no amount is selected
}
});
return (
...
);
};
When we land on page 2 I have the amount
but when you do a page refresh it directs you back to page-1
even when there is an amount
set. It works fine when you go straight to page-2
, without selecting an amount on page-1
, it re-directs you back to page-2
.
I might be getting something wrong here in how the Context API and or useRouter and useEffect work in this case.
Here is a CodeSanBox example with the issue:
https://codesandbox.io/s/next-js-userouter-with-useeffect-6645u?file=/pages/page-2.js