Migrating from formik and yup
#51
W1ckh3ad
started this conversation in
Show and tell
Replies: 1 comment
-
|
Thanks for sharing - looking forward to hearing your updates. Form Provider has been added, creating a release for that later tonight. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello there,
I will share my thoughts and results from my migration
formik&yupto@ts-react/formIntro
Some information about me and my workplace:
I am building a web platform handling different tasks (on this project since august 2021).
These applications are heavy on tables, interactivity, and forms.
At first I've passed every prop to every field from the
renderfunction informik, this got tricky after using moreweb componentswith differentcustom events.Forms got an upgrade after I've implemented the
useFieldhook fromformik, this resulted in four fewer props for each field in every form.useFieldtakes thenameprop and provides these four props. The drawback: it's not typesafe, it's easy to misspell one name and it doesn't throw an error. (Yes I could wrapuseFieldand throw ifvalueis null or undefined, but we often init with many undefined values) Additionally it's still repetitive with the schema and the usage of these components.Concerning
tablesit was much more boilerplate, but after discovering @tanstack/table I was hyped, this library blew my mind. It immensely reduced the code creating tables, but I was still missing a better solution forformsFor one of these projects (migration from an asp.net core web app with
jquery) I needed aformwith over 30 fields, and editing this form in asp.net core was annoying, it has 3 similar implementations but they were in different files. I started thinking, how about bindingcomponentsto properties in an object and giving it a prop to render maybe different components or change propsnaive implementation
The fields got all an alias, buts its matching in this snippet.
This worked but wasn't shipped, I've paused this topic until, I've discovered @ts-react/form while scrolling twitter, thanks theo :D.
My expectations
create an huge map and only create the schemas for each
formwith agrid layoutMy todos
formikcomponentsgridhelperMigrating components
My components in my `formik` directory
The differences
NOTE:
formik'sfield.onChangeconsumes aneventandreact-hook-formconsumes avalueWhat isn't migrated yet?
Focus errored field
When the
form validationthrows an error, the first field with an error gets focused, this can't be implemented as of today mentioned in #16isSubmitting state
formik provides an variable
isSubmitting, this can be set with helpers and has not any default behaviour.I wrapped the onSubmit handler in `formik` and returned the component with this handler
First Edit:
Accessing FormState
There isn't any
FormProviderprovider yet ( #31, #33 )Example: Allowing to mutate other fields value
Can't pass a custom
onChangewith editing some other fields valueDependent Field Props
The
propsof onefieldcan be dependent on the value of another field. Like setting a span of twodateswhere one can't be higher than the other.Wrapping some fields inside an custom
componentI have usecases, where some fields are wrapped inside an accordion.
Multistepform
After I sucessfulle implemented all features for one
form, I will try theMultiStepFormHelper for Array rendering
Select which load options with
react-queryhook as propsI' ve already build for another feature in this project a provider, which gets an config, creates a
fetchFnand has as the value the customreact-queryhook.The components calls with
useContextthe provider and call the returned function, this returns theObeserverResult.Remembering this, it should be possible to pass an query hook as prop to components.
More coming soon :)
Beta Was this translation helpful? Give feedback.
All reactions