Next.js Discord

Discord Forum

How to redirect/reload using Router

Unanswered
Eastern Phoebe posted this in #help-forum
Open in Discord
Avatar
Eastern PhoebeOP
hey guys I am using NextJS 12.3.1 and I am using the router as:
const router = useRouter();

then I need to perform a redirect/refresh to the website HomePage and I am doing it as follows:
router.push('/').then(() => {
  router.reload();
});

is this correct? It is working fine but I want to be sure I am doing it correctly

16 Replies

Avatar
Polar bear
router.refresh()
?
I don't understand what you want to do. You can use what I mentioned above to fetch the information again while preserving the client state.
Avatar
Eastern PhoebeOP
@Polar bear I want to redirect to the home page aka / and then refresh the page content. For some reason the content does not get refreshed which is I added the reload, that's what I am trying to do
Avatar
Polar bear
Does the content you want to renew come from the API?
Avatar
Risky did start the fire 🙃
i was also going to ask why you haven't updated to v13? (still can keep pages dir)
Avatar
Eastern PhoebeOP
if you mean I have to make a call to fetch the content, then yes
We've an upcoming release next month and do not have time to made the upgrade right now, we will, after this gets released to PRD and everyone is "happy"
Avatar
Polar bear
For this, you should use router.refresh() and a new request will be sent to the server side.
Avatar
Eastern PhoebeOP
do you mean ...
router.push('/').then(() => {
  router.refresh();
});
Avatar
Polar bear
try it
Avatar
Eastern PhoebeOP
what I was asking at first was about the syntax being right or wrong or if there is a better way to write the same thing
like chaining calls or something like that
Avatar
Polar bear
I don't know what kind of structure you work in, so I can't give you information about the best practice.
You have to find it by trying and testing
No one knows what your project needs but you
Avatar
Eastern PhoebeOP
@Polar bear FYI there is no refresh method in NextJS 12.3.1 router