TRPC response data changing to undefined when typing in a form field.
Hello,
I'm currently having a very strange issue, I am using a form to input some data and send off to a trpc endpoint to retrieve some data from the server when I click the submit button. However, the respnose
userData
variable is being changed to undefined whenever I type into the form field. I am not sure why this is happening as I do not mutate userData anywhere, and there are no new TRPC queries running (verified checking network tab) and the TRPC endpoint is set to disabled. So it will only fetch data once I call the refetch function... Anyone have any idea?2 Replies
And as an extra question, is this method of data querying a best practise? I feel like for data querying like this a useMutation is better suited, even though mutations are for changing data not querying... the syntax for mutations is much better suited for this scenario I believe.
As I mentioned in https://discord.com/channels/867764511159091230/1248003136543522876, it is likely changing to undefined because
form.getValues("userId")
is changing every time you type and the query cache only has a value for userData
for the value that is in the box when you hit submit. You should have your submit button set something in react state and use that for your userId
parameter to the query. That way the value will be stable and only change when submit is pressed.