20+ years web dev | Full-stack architect | AI integrator | Passionate about clean code, APIs, and docs | Building innovative SaaS with AI | Open source enthusiast
I have a suggestion. I'd completely remove the usage of refs to start with. They make code really hard to follow and cross-connect everything. It works fine without refs :)
Lets start with the input component. It gets everything feeded from the outside:
Nice, clean and simple. If an error gets passed to the component, it will display one. If the error is taken away, it vanishes. I'd add more things like an id, name and other things but lets leave them out for the sake of a simple example.
The dialog uses a hook named useFormSystem which takes the target URL for the form data and maybe a callback function to call so components further up in the tree might close the dialog after successful submission.
This is a possible approach I would choose. Everything is written from the top of my head and has not been tested but I wrote it to give a basic example without refs. And something that is highly re-usable.
It can be easily extended so you can pass a TS interface to type your complete form data here. You might want to use constants instead of writing "email" repeatingly in the getters.
You might want to use client-side validation to not being forced to send everything to the server to get some feedback.
OMG, you are right. I really like your solution. And I believe it is the really best solution! We can try to create our hook to re-use. The basic components are just renders and send event to their parent.
20+ years web dev | Full-stack architect | AI integrator | Passionate about clean code, APIs, and docs | Building innovative SaaS with AI | Open source enthusiast
I have a suggestion. I'd completely remove the usage of refs to start with. They make code really hard to follow and cross-connect everything. It works fine without refs :)
Lets start with the input component. It gets everything feeded from the outside:
Nice, clean and simple. If an error gets passed to the component, it will display one. If the error is taken away, it vanishes. I'd add more things like an
id
,name
and other things but lets leave them out for the sake of a simple example.Lets go to the dialog next:
The dialog uses a hook named
useFormSystem
which takes the target URL for the form data and maybe a callback function to call so components further up in the tree might close the dialog after successful submission.Lets see how that custom hook is built:
This is a possible approach I would choose. Everything is written from the top of my head and has not been tested but I wrote it to give a basic example without refs. And something that is highly re-usable.
It can be easily extended so you can pass a TS interface to type your complete form data here. You might want to use constants instead of writing
"email"
repeatingly in the getters.You might want to use client-side validation to not being forced to send everything to the server to get some feedback.
OMG, you are right. I really like your solution. And I believe it is the really best solution! We can try to create our hook to re-use. The basic components are just renders and send event to their parent.
I will update my post! Thank you very much!
I'm glad I could help out. My solution is just a quick sketch of what can be done with hooks. Its certainly far from being the best solution.
But I am happy I could give you some new perspective :)