trpc useQuery adds "?" to the url every fetch, refreshing the page and resetting the state
I am using create-t3-app with all the included "addons"
what happens is when I fill in the form, and submit it,
createAccount(email, flags)
is called, it then loads and the page is refreshed
the url changes from localhost:3000/admin
to localhost:3000/admin?
the only reason this is an issue is because of how im handling the panel with different "pages" and tabs with the navbar, stored in useState
which is reset when the page refreshes making the user have to click the Accounts
tab again
anyone know what I am doing wrong?Solution:Jump to solution
submitting a form natively "reloads" the page, because it is meant to "send data" by requesting the "next page". The "next page" can actually be configured with the
action
attribute on the form. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#action
I'm putting lots of quotes everywhere because it's how we used to do things in the olden days of the web. Now if you don't want this behavior you should add a event.preventDefault()
on the submit
event of the form (which you might already be using to trigger your mutation so it should be straightforward)
```jsx...: The Form element - HTML: HyperText Markup Language | MDN
The HTML element represents a document section containing interactive controls for submitting information.
4 Replies
Solution
submitting a form natively "reloads" the page, because it is meant to "send data" by requesting the "next page". The "next page" can actually be configured with the
action
attribute on the form. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#action
I'm putting lots of quotes everywhere because it's how we used to do things in the olden days of the web. Now if you don't want this behavior you should add a event.preventDefault()
on the submit
event of the form (which you might already be using to trigger your mutation so it should be straightforward)
: The Form element - HTML: HyperText Markup Language | MDN
The HTML element represents a document section containing interactive controls for submitting information.
its a form
thats why
thank you so much
also just for anyone who happens to read this,
is how im using the useContext
and .invalidate() in the endpoint the "proper" usage for refetching the data?
Yes, it's the eight manner like it did in the doc: https://create.t3.gg/en/usage/trpc#optimistic-updates