Get router singleton
Answered
Southeastern blueberry bee posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Southeastern blueberry beeOP
is there a way to get an instance of the client router so i can call
router.refresh()
inside a typescript file?Answered by Alfonsus Ardani
No, you have to pass the router hook into your ts function
25 Replies
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
No, you have to pass the router hook into your ts function
Answer
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
you can just import it into your all files if nessary, at least in nextjs app dir ... im not sure if i understand the question...
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Southeastern blueberry beeOP
i made a hook:
that i call whenever i make an api call that will cause client router cache data to be invalid, but this is getting really annoying, so i wanted to move this invalidation to the function that calls the library itself, i have a class that wraps all the api calls so i'd put that there instead
export function useClientCacheInvalidator(){
const router = useRouter()
return useCallback(() => {
router.refresh()
}, [router])
}
that i call whenever i make an api call that will cause client router cache data to be invalid, but this is getting really annoying, so i wanted to move this invalidation to the function that calls the library itself, i have a class that wraps all the api calls so i'd put that there instead
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
just call router.refresh() manually... it makes so much more sense...
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
its fine as long as you follow the rule of hooks
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Southeastern blueberry beeOP
what do you mean
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
like why can't you just run .refresh after your api req?
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Southeastern blueberry beeOP
that's what i'm doing, but i'm doing it inside the component itself, i wanted to move the logic to the class that handles the api calls so i don't have repetition and i don't forget to call the function
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
hmm ok, and whats the issue?
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Southeastern blueberry beeOP
i'd need a reference to the router instance in that typescript file
so i was asking if there was a global instance that i could use for that
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
but isn't .refresh is a but wasteful to just remove cache
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Southeastern blueberry beeOP
what's the alternative?
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
hmm i can't find anything simple ðŸ˜
wdym by global instance? you want it auto applied to all functions?
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Southeastern blueberry beeOP
class ApiHandler{
...
createPost(data){
const created = await fetch(...)
router.refresh()
return created
}
}
export const apiHandler = new ApiHandler()
then in my component
function Something(){
async function createPost(){
const data = await apiHandler.createPost(...)
//i don't have to worry about clearing the cache manually here
}
return <>
...
</>
}
`![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
yeah that look good
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Southeastern blueberry beeOP
yeah, but i need the router instance xD
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
o.O you can't create it there?
ig you can make it an input to createPost and let TS get angry if not inputted
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Southeastern blueberry beeOP
at that point it's like doing it manually but u are forced to pass it along
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
but it satifies your requirement of not forgeting
🙂
id say its better underneath tho
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Southeastern blueberry beeOP
i guess for now i'll make a context that handles the api wrappers and it calls a function to set the router instance so i can use it there