question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a `useField` hook to be used in custom components

See original GitHub issue

As of now, when adding custom components to the form, there is no easy way to customize them based on the form state. We have the renderField prop but that leads to verbose code to keep the layout for different field types intact.

Part of this is adding a context to allow RHF’s useFormContext to be used (see #37). This doesn’t include the individual field states though (defined in https://github.com/SeasonedSoftware/remix-forms/blob/main/packages/remix-forms/src/createForm.tsx#L47-L62).

A relatively simple solution would be to add something like a FieldContextProvider around each field, where the props of the Field type are passed through. This way, custom components (input, but also label etc) could easily implement things like error and required indicators, like such:

const MyInput = (props) => {
  const { errors } = useField();
  return <input {...props} className={errors ? 'border-red-600' : 'border-gray-200'} />;
}

const MyLabel = ({ children, ...props }) => {
  const { required } = useField();
  return <label {...props}>{children}{required ? ' *' : ''}</label>;
}

Basically this will allow the customization that the render props have currently, but take it a step further and make it easier to have customizations applied by default.

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
danielweinmanncommented, Dec 19, 2022

1.4.0 released it 🎉

Thank you, @bvangraafeiland, for the great contribution!

1reaction
bvangraafeilandcommented, Dec 13, 2022

Actually ended up not exposing the context provider at all, so there was no need for an intermediate component. The context provider is applied directly on the field component now.

Read more comments on GitHub >

github_iconTop 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 >
useField() hook - Formiz
Allows you to put a key on elements that you want to be reinitialize when the from is reset. Can be useful for...
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 >
How I built a useField React Hook for forms... and ... - YouTube
In this stream, I'll walk you through how I built a useField hook using nothing but React Hooks. I'll also talk about why...
Read more >
useFieldArray - Simple React forms validation
Custom hook for working with Field Arrays (dynamic form). The motivation is to provide better user experience and performance.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found