I've got a form with various fields used to build out the search parameters to query a DB. To preserve the state, I was imagining using query parameters like
mysite.com/?search=somestring&filter=xyz...
Using next.js with react-hook-form, I'm struggling with how to actually submit in a way that updates the URL. In old fashioned forms, the URL would automatically be updated with the fields names/values on submit, but I'm not clear how to do this in the "NEXT.js" way.
I've tried a few approaches so far:
-
in the form
onSubmit, building the URL and then doing a router.push. Doing this seems to bypass the way next.js is supposed to work. For example, my loading.tsx file doesn't display while the page refreshes. -
I tried creating a bunch of hidden inputs that store the various search parameters (e.g. search, filter, sort, etc). This way I can use fancy form elements that can handle arrays, etc, but the hidden inputs keep the URL friendly values. Then I just let a standard html
<form method="get" />do the default submit. This kind of works, but it means I'm basically fightingreact-hook-formand it doesn't feel right. -
A third option is to avoid using the URL to maintain the state and instead use session storage. There are some downsides to this I was hoping to avoid. One example is the simple ability to copy/paste a URL or bookmark it.
Am I thinking about this wrong?
