Next.js Discord

Discord Forum

How to make a home button that clears everything?

Answered
berkserbet posted this in #help-forum
Open in Discord
I am able to remove all the query parameters when the button is clicked, but some client components don't go back to their default state. How can I make that happen?
Answered by Ray
try to set a key for the searchbox like this
<SearchBox key={JSON.stringify(searchParams)} />
View full answer

10 Replies

@B33fb0n3 You can use route.refresh to clear the browser cache
Only issue with that is that I actually want to change the route at the same time. The new route needs to be '/'
@berkserbet Only issue with that is that I actually want to change the route at the same time. The new route needs to be '/'
You can do both:
youfunction() {
… do your stuff
router.push(„/„) // route
router.refresh() // clear cache
}
@B33fb0n3 You can do both: youfunction() { … do your stuff router.push(„/„) // route router.refresh() // clear cache }
It didn't seem to accomplish what I want, my searchbox still has the previous information in it
@berkserbet It didn't seem to accomplish what I want, my searchbox still has the previous information in it
try to set a key for the searchbox like this
<SearchBox key={JSON.stringify(searchParams)} />
Answer
so when the searchParams is changed, the SearchBox will rerender with default state
Cool, let me try that
That worked, thank you @Ray!
@berkserbet That worked, thank you <@743561772069421169>!
nice, also, you may not need router.refresh() there