Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react select with react hook form cotroller

function Yourcomponent(props){

    const methods = useForm();
    const { handleSubmit } = methods;
    

    const options = [
     { value: '1', label: 'Apple'},
     { value: '2', label: 'Ball'},
     { value: '3', label: 'Cat'},
    ];
    
    const default_value = 1; // you can replace with your default value
    

    // other codes of the component 
    

    function submitHandler(formData){
    
    // values are available in formData

    }


    return(
      <div>
        
        {* other part of your component *}
        <form onSubmit={handleSubmit(submitHandler)} >

           {* other part of your form *}
            <Controller
                control={methods.control}
                defaultValue={default_value}
                name="field_name_product"
                render={({ onChange, value, name, ref }) => (
                    <Select
                        inputRef={ref}
                        classNamePrefix="addl-class"
                        options={options}
                        value={options.find(c => c.value === value)}
                        onChange={val => onChange(val.value)}
                    />
                )}
            />

           {* other part of your form *}
        </form>

        {* other part of your component *}
      </div>
    )
}
Comment

react select and react hook form

import Select from "react-select";
import { useForm, Controller } from "react-hook-form";

const {
  control
} = useForm();

<Controller
  control={control}
  defaultValue={options.map(c => c.value)}
  name="options"
  render={({ field: { onChange, value, ref }}) => (
    <Select
      inputRef={ref}
      value={options.filter(c => value.includes(c.value))}
      onChange={val => onChange(val.map(c => c.value))}
      options={options}
      isMulti
    />
  )}
/>
Comment

PREVIOUS NEXT
Code Example
Javascript :: match ids from 2 arrays in javascript asynchronous programming 
Javascript :: js array get index 
Javascript :: what is template engine in express 
Javascript :: react native password strength meter 
Javascript :: console javascript 
Javascript :: Error occurred while trying to proxy to: localhost:3000/ 
Javascript :: javascript factorial of a number 
Javascript :: java object to json 
Javascript :: express post not working 
Javascript :: filter an array of objects and match its key with values inside another array 
Javascript :: networkx check if node exists 
Javascript :: adding function to objects js 
Javascript :: change the value in checkbox by button react 
Javascript :: parse data from url javascript 
Javascript :: iterate through an array 
Javascript :: how to open a new browser window using a .bat 
Javascript :: fullcalendar react add event duration 
Javascript :: jest debugger node 
Javascript :: js combine 2 array to object key value 
Javascript :: magento 2 translate js 
Javascript :: next js page 
Javascript :: deleteone mongoose 
Javascript :: Sort an array to have specific items 
Javascript :: js create json from object 
Javascript :: jquery validation on click 
Javascript :: document fragment 
Javascript :: int val javascript 
Javascript :: identify primary colors in img with js 
Javascript :: search string for character javascript 
Javascript :: show password fa-eye javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =