Next.js Discord

Discord Forum

RevalidatePath with useRef

Unanswered
Siamese posted this in #help-forum
Open in Discord
SiameseOP
Hi! I'm looking for advice of how to reset useRef state is the page is revalidated by the revalidatePath function. I use
const loaded = useRef(false);
to check is page rendered or not on initial render and after revalidating it, I'd like to re-run the code under the useRef, is it possible?

2 Replies

Siberian Flycatcher
Hi 👋

Can you give a bit more info? What are you trying to achieve? Could you show more code?
SiameseOP
I have a client component which uses this code as an indication is a component rendered or not
const loaded = useRef(false);

if (!loaded.current) {
some code
loaded.current = true;
}

So the code inside of the if statement executes only in case when component mounts and won't execute until it cycle through unMount and mount again.
I use it as a child for server component and my goal is to be able to run this code on a specific route after I call
revalidatePath(path)
from server action. This behaivor can be acheived now by using
router.refresh()
but I'd like to be able to use it without hard refresh, because server component do the refresh and the new data fetched.