I'm trying out the new actions with react router and am trying to learn to love "using the platform" for data, FormData. I'm coming from react query mutations where i keep form data in state so this is a bit different and...not as nice? Some warts I'm running into with FormData:
- No type safety around data structures. I can't enforce FormData to have certain fields.
- It's really hard to debug the state of the form.
console.log(formData)is useless. - Everything is a string. With js/json you have booleans, strings, numbers, arrays, null, etc. I can pass it to zod and easily validate the data
- Most of the time i need to create a js object from the form data. There does not seem to be a good way to do this especially since everything is a string.
- Using form names for complex data is awkward,
<input name="person[0][firstName]" />. Since all FormData is only one level deep you end up having to convert into complex data yourself. - You still need to end up maintaining state for allowing the user to add inputs:
<button>Add Receipt Line Item<button> - react is all about rendering ui as a function of state. that's what makes react so pleasant to work with. Using
defaultValue, only form elements with names, and relying on generatedFormDataseems to veer away from that paradigm
I know the web platform has all the necessary pieces but using FormData seems like a huge, unnecessary crutch to me right now. Is everybody using some package that makes this more productive, opaque? I can't seem to find what makes this kool-aid palatable. Anyone have some tips that make using FormData more pleasant?