6

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 ?

7
  • what version are you using Commented Sep 21, 2018 at 4:52
  • ` "react-select": "^2.0.0"` Commented Sep 21, 2018 at 4:53
  • defaultValue should be 55 only in your case. As defaultValue is selected option. Commented Sep 21, 2018 at 4:56
  • @PardeepDhingra I already tried this like defaultValue="55", its not working . react-select taking whole object as option, like this {value: "55", label: "My Sensor_56"} . Commented Sep 21, 2018 at 4:58
  • I tried an example with react-select value={{value: "55", label: "My Sensor_56"}} working well for me, Commented Sep 21, 2018 at 5:19

1 Answer 1

6

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()}
      />
    );
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

is it your answer ? like as you mentioned in answer its already working . i want to know how to set default value
This is the example working for me . to set default value value={ this.organizationOptions()[1] }
Welcome @Vishal
@PardeepDhingra what if you want to set the default value as what you have typed in and it's not present in the option array . How do you go about that?
Does this set the selected label as well?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.