[Noob] Not understanding how to create an edit form.
I have 2 major questions/problems, full noob to thank you for any guidance.
I am trying to create an edit form for already submitted data, profile related data like username, description etc. During this process, a user picks a URL choice.
The edit form is using the same base, with some removed. This includes
slug
being removed.
This is where my questions begin:
1) Because Slug is required, I was getting issues where the default value being loaded in doesn't really work. I am struggling to understand how I load in the data for an edit form to then be re-submitted. Any examples of correct ways to load in data on a page to then be re-submitted?
2) When submiting data during testing, I would get errors related to slug not being included, or already being added. I am using mutate inside trpc so I am again feeling like I am missing some understanding.
Thank you!15 Replies
im a bit confused on what the problem your running into is
is the issue on the backend (input/logic of something inside a trpc procedure) or frontend (something with the form behavior/ state)?
Form state, I wrote something stupid so have decided to try out React Hook Form. I am now not understanding this error in particular -
I've removed imports etc due to space, sorry for the big block, but I think its needed for context based on what I've been reading?
My understand is the hydration? of the page has to be done on the page at once, and my useForm query is trying to do it more than once.. But I don't understand how I change this in a way that gathers the information correctly.
I've got the data, all tested and it outputs when its just placed in logs or divs, but making the edit form update with the data, that I struggle with!
https://reactjs.org/docs/hooks-rules.html giving this a read now in hopes I understand better 😅
Rules of Hooks – React
A JavaScript library for building user interfaces
Same error again, I just feel so lost. Like I am way off the beaten path here..
I want to be able to fill a form with the users previous data, that they can edit and re-submit. Adding data in from a blank slate I got, manipulating the DOM correctly I am not
Alright, rubber duck time.
Removing the useEffect() allows the page to load without errors, but of course no data is loaded from userQuery. BUT the data is loaded as I see the form, otherwise I would get the
if (!userQuery.data)
So I need to load in the values that are now loaded.
OK!
So even in this example. Its triggering the same error, so its not TRPC specific. I still don't understand the cause, but ANY value causes the runtime errorsorry for late response I was out
appreciate you sending all this code, tbh I dont understand it however I can show you some examples of forms ive made and maybe those will help?
also definitely use julius's zod wrapper for RHF: https://github.com/juliusmarminge/t3-complete/blob/main/src/utils/zod-form.ts
if this is helpful at all great, if not lmk and I can look further into your specific case
GitHub
t3-complete/zod-form.ts at main · juliusmarminge/t3-complete
Contribute to juliusmarminge/t3-complete development by creating an account on GitHub.
I've found a solution!
A lot of the googling was on how how using useEffect() is the better way to do it, not sure if that advice works with t3 well but it does resolve my issue
nice!
So I create the Form, then only once I have the data (the bit I kept getting wrong) does useEffect() trigger and that creates the values in the fields
I see now that looks good to me
I've noticed it resets when I click off/on the window, but I am guessing that's just because it's dev mode?
where is the userQueryData coming from
Or does useEffect trigger when window is refocused?
another component?
this is almost for sure react querys refetch on window focus
So I grab the data from db with trpc, then populate the fields
Might have to revisit this in the future, but for now I am just happy to have SOMETHING and move on 🤣
A smarter mind will come back and re-do it all I'm sure, once I've learned a bit more..
Thank you for the help!
sounds good feel free to ping me if you need anything else