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 :: javascript parse json 
Javascript :: js json to object 
Javascript :: nodejs wait function 
Javascript :: proactive vs reactive 
Javascript :: remove one array from another javascript 
Javascript :: check date format javascript 
Javascript :: postcss.config.js 
Javascript :: js maximum number value 
Javascript :: javascript add class 
Javascript :: throttling function in javascript 
Javascript :: jquery form validation plugin callback function 
Javascript :: how to send header in axios 
Javascript :: getusermedia example 
Javascript :: if array has multiple duplicate value number them accordingly 
Javascript :: angular elementref 
Javascript :: react navigation navigator types 
Javascript :: fetch post 
Javascript :: settimeout in a for loop javascript 
Javascript :: react run on route change 
Javascript :: Vue use props in style 
Javascript :: sequelize delete item 
Javascript :: pagination in strapi 
Javascript :: react native text input right 
Javascript :: settext javascript 
Javascript :: js indexof regex 
Javascript :: nestjs version 
Javascript :: regular expression javascript password strength 
Javascript :: node convert buffer to string 
Javascript :: android force date inside RecyclerView to re render 
Javascript :: swal con input 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =