Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

REACT-SELECT defaultValue in CustomDropdown not working

I want the default value of my dropdown to be defaultValue={item.taste} (value from match.json) but it’s not working… (go to /menu/Menu1 and Pea Soup)

import Select from "react-select";

export default function CustomDropdown({ style, options, defaultValue }) {
  return (
    <div style={style}>
      <Select options={options} defaultValue={defaultValue} />
    </div>
  );
}

MenuItemDisplay:

export default function MenuItemDisplay() {

  const { menuId, itemId } = useParams();
  const { match } = JsonData;    
  const matchData = match.find((el) => el._id_menu === menuId)?._ids ?? [];
  const item = matchData.find((el) => el._id === itemId);

  const styles = {
    select: {
      width: "100%",
      maxWidth: 150
    }
  };
  const TASTE = [
    { label: "Good", value: "Good" },
    { label: "Medium", value: "Medium" },
    { label: "Bad", value: "Bad" }
  ];
...   

  return (
    <>
        <div className="TextStyle">
          {"Taste "}
          <CustomDropdown
            style={styles.select}
            options={TASTE}
            defaultValue={item.taste}
           //The default value is not working only if it's 
           //TASTE[0]
          />
        </div>
        ...
    </>
  );
}

Here the link for the code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As defaultValue you need to pass one of the objects of the TASTE array. You can do this:

<CustomDropdown
  style={styles.select}
  options={TASTE}
  defaultValue={TASTE.find(t => t.label === item.taste)}
/>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading