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
-
Hello there,
I will share my thoughts and results from my migration
formik
&yup
to@ts-react/form
Intro
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
render
function informik
, this got tricky after using moreweb components
with differentcustom events
.Forms got an upgrade after I've implemented the
useField
hook fromformik
, this resulted in four fewer props for each field in every form.useField
takes thename
prop 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 wrapuseField
and throw ifvalue
is 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
tables
it 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 forforms
For one of these projects (migration from an asp.net core web app with
jquery
) I needed aform
with 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 bindingcomponents
to 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
form
with agrid layout
My todos
formik
componentsgrid
helperMigrating components
My components in my `formik` directory
The differences
NOTE:
formik's
field.onChange
consumes anevent
andreact-hook-form
consumes avalue
What isn't migrated yet?
Focus errored field
When the
form validation
throws 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
FormProvider
provider yet ( #31, #33 )Example: Allowing to mutate other fields value
Can't pass a custom
onChange
with editing some other fields valueDependent Field Props
The
props
of onefield
can be dependent on the value of another field. Like setting a span of twodates
where one can't be higher than the other.Wrapping some fields inside an custom
component
I have usecases, where some fields are wrapped inside an accordion.
Multistepform
After I sucessfulle implemented all features for one
form
, I will try theMultiStepForm
Helper for Array rendering
Select which load options with
react-query
hook as propsI' ve already build for another feature in this project a provider, which gets an config, creates a
fetchFn
and has as the value the customreact-query
hook.The components calls with
useContext
the 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