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.

Select component not working with react-hook-form ref should be forward to input instead on styled box

See original GitHub issue

<Select /> ref should be forward to input instead of <Box />

Expected Behavior

React-hook-form use ref to register input fields. When submitting form <Select .../> selected value should be in the form data

Actual Behavior

Select property value is missing from data received into onSubmit function

URL, screen shot, or Codepen exhibiting the issue

https://codesandbox.io/s/amazing-driscoll-m08if

Steps to Reproduce

Fill all the fields in the form and submit

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Alkindi42commented, Feb 23, 2021

Hello,

You can use Controller component:

<Controller
  name="gender"
  control={control}
  defaultValue="Female"
  render={({ onChange, value }) => (
    <FormField
      htmlFor="gender"
      label="Gender"
      error={errors.gender && errors.gender.message}
    >
      <Select
        name="gender"
        defaultValue={value}
        options={["Female", "Male"]}
        onChange={({ option }) => onChange(option)}
        />
      </FormField>
  )}
/>

It’s not a perfect solution but it works.

1reaction
disbeliefcommented, May 15, 2021

Thanks so much @Alkindi42! A few tweaks to your snippet got it working for me. I’ll post my slightly modified version here (maybe a recent change to react-hook-form API is the reason for the difference):

const options = [
  { label: 'brown', value: '1' },
  { label: 'blond', value: '2' },
  { label: 'grey', value: '3' },
  { label: 'blue', value: '4' },
];

return (
  <Controller
    name="hairColor"
    control={control}
    render={({ field: { name, value, onChange }, fieldState }) => (
      <Select
        {...fieldState}
        name={name}
        options={options}
        value={value}
        labelKey="label"
        valueKey={{ key: 'value', reduce: true }}
        onChange={({ value: nextValue }) => onChange(nextValue)}
      />
    )}
  />
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Returning correct value using react-select and react-hook-form
Everything works fine except the returned value is coming back as the string "[object Object]" instead of the fullName property from the object....
Read more >
FAQs | React Hook Form - Simple React forms validation
React Hook Form relies on uncontrolled form, which is the reason why the register function capture ref and controlled component has its re-rendering...
Read more >
React Hook Form: A guide with examples - LogRocket Blog
Learn all about using forms in React with React Hook Form, including how to create and validate forms — even with third-party components....
Read more >
How to create reusable form components with React Hook ...
Step 1: Create An Input Component ... The first step is to create an input component. Creating an isolated component can be a...
Read more >
react-dropzone
Important: if you omit rendering an <input> and/or binding the props from getInputProps() , opening a file dialog will not be possible. Refs....
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