react-phone-number-input: Can't figure out how to use this with Formik field component
I’m using Formik and trying to use this package, but Formik can’t see the value of the input and thus doesn’t validate or submit the form.
Here’s the PhonInputField component that I’m using …
import PhoneInput from "react-phone-number-input";
const PhoneInputField = ({
field,
form
}) => {
return (
<div className="input-field">
<PhoneInput
placeholder="Enter phone number"
name={field.name}
value={field.value}
onChange={field.onChange}
onBlur={field.onBlur}
/>
</div>
);
};
export default PhoneInputField;
And here’s how I’m using it in Formik …
<Formik
initialValues={initialValues}
validationSchema={signInSchema}
onSubmit={handleFormSubmit}
>
{({
isValid,
isSubmitting,
handleChange,
handleBlur
}) => (
<Form>
<Field
type="tel"
name="phone_number"
component={PhoneInputField}
/>
<div className="cta">
<button
type="submit"
disabled={!isValid || isSubmitting}
className="btn btn-primary big"
>Submit</button>
</div>
</Form>
)}
</Formik>
What am I doing wrong here?
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 26 (10 by maintainers)
If it helps, here is my phoneinput/formik control
In case if anyone needs help this is my current implementation. Below approach works with both onChange and onBlur events and doesn’t throw error when string is given in field (disabled). Thanks to above replies:
@catamphetamine please find a working implementation for future reference. https://codesandbox.io/s/formik-react-phone-number-input-p5jvs
I have modified @AndrewStratigos 's version. This version works right with Formik’s onBlur, allowing you to validate the field on blur.
@AndrewStratigos That did actually work, Thanks a lot man. The trick in your code was this part …
Again, thanks a bunch 👍
Thank you so much. This helped me. Apparently, the trick was the
formik.setFieldValuemethodThanks a lot MidhaTahir Now I have finally passed the issue from react-phone-input-2😊😊
@ackalhan I’ve updated the README with a
react-hook-formsection. https://github.com/catamphetamine/react-phone-number-input#react-hook-form@boriswinner Ok, what are the main issues when using this library with
formik“out of the box”?Perhaps we could add a section in the readme on using this library with
formikor other libraries.remove
onChange_and just call in your jsxonChange={formik.handleChange(name)}same with onBlur if you need validation on blurI may be late to the party but for anyone else here having issues with their onChange handler, remember this also comes with a curried option, in a regular web input formik can take the name attribute from the target it receives in the change handler, React-native generally doesn’t provide a synthetic event (it does but its different) most people tend to use the
onChangeTextproperty instead so formik needs other queues to know how to update the form’s state, consider the following:instead of calling the change (or blur) handlers like we do on web:
onChange={formik.handleChange}you call it with the attribute name:
onChange={formik.handleChange('phoneNumber')}this also works for the blur handleronBlur={formik.handleBlur('phoneNumber')}also make sure you send the change values as string or formik won’t accept it, it sometimes is a pain in the ass to work forms on RNPerhaps the reason could be
formikexpectingonChange(event)instead ofonChange(value). See if something likeonChange={value => field.onChange({ target: { value } })}would work.