0

I am trying to use react select and trying to set a defaultValue but when I set a default value it does not appear on my dropdown. I have tried everything and then came here for help. any help would be appreciated. below attached is the code :

<Select
  classNamePrefix="react-select"
  options={
    countryDetails
      ? countryDetails.map((c) => ({
          label: c.label.split("-")[0],
          value: c.value
        }))
      : [{ label: "", value: "" }]
  }
  name="operatingCountry"
  placeholder={t("Select Country")}
  components={{ ValueContainer: CustomValueContainer }}
  defaultValue={
    (console.log(
      "country boy",
      countryDetails.find((c) => c.value == defaultCountryUser)
    ),
    countryDetails.find((c) => c.value == defaultCountryUser))
  }
  value={this.props.basicInfo["operatingCountry"].value}
  onChange={({ label, value }) => {
    this.props.selectChanged(
      "operatingCountry",
      { label, value },
      sectionKey
    );
  }}
/>

the console in the defaultValue gives result in console. as an object {label:"two",value:2}

1 Answer 1

1

The value should be an object. Try changing value to this:

value={this.props.basicInfo["operatingCountry"]}
Sign up to request clarification or add additional context in comments.

2 Comments

this value = {this.props.basicInfo["operatingCountry"].value} already returns me the object
What is the type of defaultCountryUser. Objects in JavaScript always returns false when compared.

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.