`useField()` hook
See original GitHub issueFeature Request
It’d be nice to have a useField()
hook for the existing react-final-form. This would give basically the same performance as <Field>
, but with data returned from the hook instead of passed to the render prop.
useField(props: FieldProps): FieldRenderProps
The idea would be to get the form store via context, then subscribe to it within a useEffect()
, and update a useState()
each time it changes. Basically, implementation would look exactly like the hooks version of <Field>
.
This would allow for fields to be created without the render prop, which would be nice when creating styled field components. For example:
function Field(props)
let { input, meta } = useField(props)
return (
<StyldeField>
<StyledLabel>Bio</StyledLabel>
<StyledTextarea {...input} />
{meta.touched && meta.error && <StyledError>{meta.error}</StyledError>}
</StyledField>
)
)
And the corresponding <Field>
based example from the README:
function StyledField(props) {
return (
<Field
{...props}
render={({ input, meta }) => (
<StyledFieldWrapper>
<StyledLabel>Bio</StyledLabel>
<StyledTextarea {...input} />
{meta.touched && meta.error && <StyledError>{meta.error}</StyledError>}
</StyledFieldWrapper>
)}
/>
)
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
useField() - Formik
useField is a custom React hook that will automagically help you hook up inputs to Formik. You can and should use it to...
Read more >The useField Hook - Formik for Beginners - Code Daily
Intro. The Field component pre-dates the release of hooks. Hooks help remove much of the complexity and helps integrate Formik into more complex...
Read more >Final Form Docs – `useField()`
The useField() hook takes two parameters: ... i.e. the component will only rerender if the field state subscribed to via useField() changes.
Read more >useField() hook - Formiz
When using the useField hook, you need to pass your component props to the hook. Then the hook gives you access to the...
Read more >How to properly use useField hook from Formik in typescript
I used the FieldHookConfig as the type for the props because the useField is expecting a string or the FieldHookConfig base on the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I played around with a version of
useField
using the current context setup ofreact-final-form
and haven’t found that to be the case. It isn’t very polished, but is there something I’m missing?Thanks for this issue, @jamesknelson. It’s very helpful to have other people attack a problem you’ve attempted to solve and to evaluate the differences.
So yes, RFFH is the package that uses hooks to use FF. What Render Props™ give you that hooks cannot – and never will be able to (somewhat validated) – is the ability to let your components (e.g.
Field
) control when they rerender. With hooks, if you are usinguseField()
, your entire form must rerender on every keypress, so the whole “optimization through subscriptions” architecture of FF is thrown to the wind. The good news is, however, that 96% of forms don’t need that level of optimizations, so hooks are great.The most amazing thing about the hooks announcement with relation to Final Form is the validation of keeping form state in a separate container, outside of React. Within hours of the hooks announcement, I was able to whip up a version of RFF with almost no effort.