How can i use "useRouter" in a Function
Answered
Giant panda posted this in #help-forum
Giant pandaOP
^^
56 Replies
Giant pandaOP
you are using an async component and client component, this will give an error... (async is only for server and useRouter is only client)
@riský you are using an async component and client component, this will give an error... (async is only for server and useRouter is only client)
Giant pandaOP
still same even tho i removed async
how are you using the components?
Giant pandaOP
import { useRouter } from 'next/navigation';
DeleteProgramFunc is called in the Table
The Red Button
and you have made sure that the page is client (with
'use client' at the top)Giant pandaOP
yep
ohhh i get it
@Giant panda Click to see attachment
you can't use components like this
i think you need to make a component for the button
and have the onclick inside that
Giant pandaOP
damn
as it isn't an actual component, and as such the hook doesn't work
Giant pandaOP
Ok thanks, imma try it out now
@riský as it isn't an actual component, and as such the hook doesn't work
Giant pandaOP
No Error and it dosnt refresh
show your code
Giant pandaOP
the toast and cookies are getting removed
but page is not getting refreshed
o.O
Giant pandaOP
I dont like Nextjs 14 ðŸ˜
if you check dev tools, there are no network requests?
Giant pandaOP
there is just no refresh for the page
also how are you using DeleteProgramFunc
Giant pandaOP
ahh because your way of getting secret_key and program won't work this way
you would need
{secret_key, program} in your function@Giant panda there is just no refresh for the page
can you try a good old window.reload and see what that does
Answer
Giant pandaOP
ok lemme try
yep works
thanks 🙂
window.reload works perfectly fine
but that isn't as good as router.refresh
Giant pandaOP
u saved me 2 days of pain
you shouldn't need to use this method tho (it is worse)... but if the other doen't work 🙂
worse as you don't get these benifits
and instead used normal js syntax for reload
but i really don't know why nextjs method doesn't work for you
Giant pandaOP
yea
no error thats also weird
but thanks 🙂
@Giant panda but thanks 🙂
no problem, and sorry that it didn't work as intended ðŸ˜
Giant pandaOP
haha not ur fault
ig you can mark this as solved, as it kinda is... but i might check later and see if its better in future release :)
Giant pandaOP
but it works perfectly
how can i mark it as solved lmao
Original message was deleted
Giant pandaOP
oh thanks
Have a nice day 🙂
you too, enjoy making your website and feel free to make new thread for new problems 🙂