1

I tried to implement with React Hook From using <Controller />. While I am submitting the form country field return undefined.

<Controller 
  name="country"
  control={control}
  render={({ field: { onChange, value } }) => (
    <ReactFlagsSelect
      selected={selected}
      onSelect={code => handleChange(code)}
      value={value}
      onChange={onChange}
    />
  )}
/>

1 Answer 1

1

The problem is you pass RHF's onChange handler to the wrong prop. <ReactFlagsSelect /> doesn't have a onChange prop, instead you should pass it to the onSelect prop.

<Controller 
  name="country"
  control={control}
  render={({ field: { onChange, value } }) => (
    <ReactFlagsSelect
      selected={selected}
      onSelect={onChange}
      value={value}
    />
  )}
/>

Side-note: RHF's will update value changes to your registered fields in it's internal form state, so there is no need to use extra state management for these values via useState or something similar. If you really need to call your handleChange callback, you can do both in the onSelect callback.

<Controller 
  name="country"
  control={control}
  render={({ field: { onChange, value } }) => (
    <ReactFlagsSelect
      selected={selected}
      onSelect={code => {
        onChange(code);
        handleChange(code);
      }}
      value={value}
    />
  )}
/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.