How to add a react-select dropdown with react-hook-form in React
In this article, we will show you how to add a react-select dropdown with react-hook-form in React. Similarly, you can implement other packages with a React Hook Form.
Here, we will continue the previous article so advise you to go through that article.
Validate a form in React using react-hook-form
Demo Application
Add a react-select dropdown with react-hook-form in React
1. Create a simple form
Refer to the following articles, If you don’t know how to create a form using react-hook-form.
2. Add a react-select dropdown in form
Here, we’ll use the react-select npm package to add the dropdown in the form.
To add the third party package, we have to use the useController
from the react-hook-form
.
1 2 3 4 5 6 | const { field } = useController({ name: 'language', control }); const { value: langValue, onChange: langOnChange, ...restLangField } = field; // OR const { field: { value: langValue, onChange: langOnChange, ...restLangField } } = useController({ name: 'language', control }); |
As you can see in the above code, we have used the control
in the useController
. So don’t forget to export control
from the useForm()
.
Now, let’s use the above exported method in the react-select
.
1 2 3 4 5 6 7 8 9 | <Select className='select-input' placeholder="Select Language" isClearable options={languageList} value={langValue ? languageList.find(x => x.value === langValue) : langValue} onChange={option => langOnChange(option ? option.value : option)} {...restLangField} /> |
3. Output
Let’s put all the code together and see how it looks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | import React, { useState } from 'react'; import { useForm, useController } from 'react-hook-form'; import { yupResolver } from '@hookform/resolvers/yup'; import * as yup from "yup"; import Select from 'react-select'; const schema = yup.object({ name: yup.string().required("Please enter name"), email: yup.string().email("Please enter valid email").required("Please enter email"), website: yup.string().url("Please enter valid url").required("Please enter url"), language: yup.number().nullable().required("Please select language"), }); const languageList = [ { value: 1, label: 'English' }, { value: 2, label: 'Hindi' } ]; function App() { const [data, setData] = useState(); const { register, handleSubmit, formState, control } = useForm({ resolver: yupResolver(schema) }); const { field: { value: langValue, onChange: langOnChange, ...restLangField } } = useController({ name: 'language', control }); const { errors } = formState; const onSubmit = (formData) => { setData({ ...formData }); } return <form onSubmit={handleSubmit(onSubmit)}> <h4>Add a react-select dropdown with react-hook-form - <a href="https://www.cluemediator.com/" target="_blank" rel="noopener noreferrer">Clue Mediator</a></h4> <div> <label>Name</label> <input placeholder='Name' {...register('name')} /> {errors.name && <p>{errors.name.message}</p>} </div> <div> <label>Email</label> <input placeholder='Email' {...register('email')} /> {errors.email && <p>{errors.email.message}</p>} </div> <div> <label>Website</label> <input placeholder='Website' {...register('website')} /> {errors.website && <p>{errors.website.message}</p>} </div> <div> <label>Language</label> <Select className='select-input' placeholder="Select Language" isClearable options={languageList} value={langValue ? languageList.find(x => x.value === langValue) : langValue} onChange={option => langOnChange(option ? option.value : option)} {...restLangField} /> {errors.language && <p>{errors.language.message}</p>} </div> <div> <button>Submit</button> </div> {data && <pre>{JSON.stringify(data, null, 2)}</pre>} </form> } export default App; |
Run the application and check the output in the browser.
I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂
Thank you so much, it helped a lot. Keep doing the nice work.
It was our pleasure to help.