Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-select does not show the selected value in the field

i have a react-select component which i define like this:

<Select
            id="portf"
            options={opts}
            onChange={value => portfolioSelector(value)}
            placeholder="Select Portfolio"
          />

with opts = [{label: any, value:1}, {label:Two, value:2}].

The values when selected are stored in the state via portfolioSelector function. The problem is that when i select a value it wasn't show in the select field. My main component is this:

const PortfolioSelector = ({
  opts,
  portfolioSelector
}) => {
  if (opts) {
    return (
      <div className="portfolio select-box">
        <label htmlFor="selectBox" className="select-box__label">
        Portfolio
        </label>
        <div className="select-box__container">
          <Select
            id="portf"
            options={opts}
            onChange={value => portfolioSelector(value)}
            placeholder="Select Portfolio"
          />
        </div>
        <div className="separator" />
      </div>
    );
  }
  return (
    <div>Loading</div>
  );
};

Do you know why?

like image 803
user7334203 Avatar asked Apr 06 '17 09:04

user7334203


1 Answers

The value is handled really bad, and it needs hacks like here, explained here.

Long story short; the value works differently. You'd expect

value={MY_VALUE}, but it works instead

value={{label: MY_VALUE}}.

like image 71
Daniel Danielecki Avatar answered Oct 07 '22 19:10

Daniel Danielecki