Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - How to set default value in react-select

I used react-select in my application for showing select box. below is code of my select.

<Select
       className="custom-form-control mb-2"
       name="organization_options"
       options={this.organizationOptions()}
       placeholder={false}
       onChange={this.handleChange.bind(this)}
       value={selectedValue === "" ? this.organizationOptions()[0] : selectedValue}
/>

Below are the options i already have in select box

0: {value: "62", label: "Dfdf"}
1: {value: "128", label: "Dfdfdsf"}
2: {value: "151", label: "Fgfdgdfh"}
3: {value: "121", label: "Hhhfas"}
4: {value: "55", label: "My Sensor_56"}
5: {value: "13", label: "New Org"}
6: {value: "44", label: "Org 2"}
7: {value: "148", label: "Testing App"}

I have the value from query string like value="55", if the value is present i want to show that selected value in select box .

I tried in 2 different ways which are shown as below code ,

1)

selectedValue='55'
defaultValue={this.organizationOptions().find(op => {
   return op.value === selectedValue
})}

2

defaultValue={{value: "55", label: "My Sensor_56"}}

But no one works, Can somebody tell me How can i set defaultV alue if already have or don't show default value if i don't have selected value ?

like image 375
Vishal Avatar asked Sep 21 '18 04:09

Vishal


People also ask

How do I set default value in React select?

You can use an attribute defaultValue to set the default value in the Select menu If none option is integrated with this attribute first option is selected by default. You can create an Array of the object where you will store all options to be displayed and any single object is passed in the defaultValue attribute.

How do I set the default option in select?

The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.

How do I set default value in React hook form?

You can set an input's default value with defaultValue/defaultChecked (read more from the React doc for Default Values). You can pass defaultValues as an optional argument to useForm() to populate the default values for the entire form, or set values on an individual Controller component via its defaultValue property.


1 Answers

Here default value we are finding out in organization options and pass it as value in Select:

import React from 'react';
import Select from 'react-select';

export default class App extends React.Component {
  organizationOptions = () => {
    return [
       {value: "62", label: "Dfdf"},
       {value: "128", label: "Dfdfdsf"},
       {value: "151", label: "Fgfdgdfh"},
       {value: "121", label: "Hhhfas"},
       {value: "55", label: "My Sensor_56"},
       {value: "13", label: "New Org"},
       {value: "44", label: "Org 2"},
      {value: "148", label: "Testing App"}
    ]
  }

  render() {
    const selectedValue = "55"
    return (
      <Select
        value={this.organizationOptions().find(op => {
           return op.value === selectedValue
        })}
        onChange={this.handleChange}
        options={this.organizationOptions()}
      />
    );
  }
}
like image 166
Pardeep Dhingra Avatar answered Sep 20 '22 20:09

Pardeep Dhingra