When an option is chosen from the dropdown menu in react-select

Recently, I encountered an issue while working with the react-select dropdown in my project. After selecting an option from the dropdown, I noticed that all the option backgrounds turned blue. Upon inspection, I found that all options had the class name "react-select-option-is-selected."

It was crucial for me to have the background color applied only to the selected option, rather than all options. https://i.sstatic.net/kWFnE.png

import Select from 'react-select';
const savedLists = [
    {
        "__typename": "List",
        "key": "3232",
        "name": "Saved List A"
    },
    {
        "__typename": "List",
        "key": "323",
        "name": "rwecrrrcrcsas"
    },  
]
const modifiedSavedList = savedLists.map(({ name: label, ...rest }) => ({
    label, ...rest
  }));

  const onChangeHandler = (selectedDestination) => {
    const searchParams = new URLSearchParams(location.search);
    const checkedProductsIds = items.filter((e, i) => checkedList.some(j => i == j)).map(obj => (obj.uid));
    const sourceUID = searchParams.get('uid');
    SetOptionSelected(selectedDestination.name)
    copyitmes(sourceUID, selectedDestination, checkedProductsIds)
  }
<Select
 className="itemListDropdown"
 classNamePrefix="react-select"
 options={modifiedSavedList}
 isSearchable={false}
 placeholder={<div>Copy to</div>}
 components={{ IndicatorSeparator: () => null }}
 onChange={onChangeHandler}
 />
.itemListDropdown {
    box-sizing: border-box;
    width: 71%;
    height: 36px;
    color: $color-medium-grey !important;
    font-weight: 400;
    margin-left: auto;

    .react-select__control {
        height: 35px;
        border-radius: 2px;
        border: 1px solid $color-medium-grey;
        text-align: left;
        padding-left: 12px;
        box-shadow: none;
    }

    .react-select__value-container {
        height: $input-box-min-height;
        align-content: center;
    }

    .react-select__value-container,
    .react-select__menu,
    .react-select__input-container,
    .react-select__menu-list {
        padding: unset;
        margin: unset;
    }

    .react-select__menu {
        box-shadow: unset;
    }

    .react-select__option--is-selected {
        background-color: $select-menu-background-hover;
        color: $color-white;
    }

    .react-select__control--menu-is-open {
        border-color: $color-medium-grey !important;
        border-style: solid !important;
        border-width: 2px 2px 1px 2px !important;
        box-shadow: none;
        border-top-left-radius: 3px;
        border-top-right-radius: 3px;
    }

    .react-select__placeholder,
    .react-select__indicator {
        margin-bottom: 5px;
    }
}

Answer №1

The reason for this issue is the incorrect structure of options provided to your react select component. In order for react-select to work properly, each option must have both a value and a label. By only providing a label without a corresponding value, react-select is unable to determine which option is selected.

To resolve this problem, consider making the following change:

const modifiedSavedList = savedLists.map(({ name: label, ...rest }) => ({
    label, value: { ...rest }
  }));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I resolve the error "Uncaught ReferenceError: No date format named: undefined" in React-Intl?

Yahoo recently released a set of internationalization tools for JavaScript, known as FormatJS. The FormatJS guide explains that dates have default formats like short, medium, long, and full. FormatJS offers support for Dust, Handlebars, and React. While ...

What is the method for retrieving a class's property without creating an object instance?

Currently, I am working with a class setup like this: class MyClass { constructor(privateInfo) { this.a = "a"; this.b = "b"; } myMethod() { return privateInfo; } } It seems that the privateInfo variable needs to be ...

Encountering issues with React Native's useEffect function while attempting to retrieve the user's authentication status from Firebase

I'm encountering an issue with the useEffect hook. Every time I attempt to retrieve the authentication state of the user following the onAuthStateChanged event, the number of calls within the useEffect becomes excessively high. Occasionally, I also re ...

Ways to customize CSS heart icon to place within an image

My latest project involves CSS artistry with a heart shape. However, I am looking to take it a step further by incorporating a dynamic image inside of it. Here is the current code snippet: .heart { position: relative; width: 100px; height: 90px; f ...

When using angular.less, the @import feature is functioning correctly but a 404 error is displayed in the

Currently, I am working on a project using AngularJS along with angular.less. To centralize all my constants, I have stored them in one .less file and imported it at the beginning of all my other less files using a relative path: @import "constants" Even ...

Choosing Links within InfoWindows in Google Maps using jQuery

I have a web application where I am utilizing jQuery to identify all the links present on the page and alter their destination so that I can smoothly transition to another section of the website using AJAX. However, I am encountering an issue with certai ...

Experiencing delays with Angular 4 CLI's speed when running ng serve and making updates

After running ng serve, I noticed that the load time is at 34946 ms, which seems pretty slow and is impacting our team's performance. Additionally, when we update our code, it takes too long to reload the page. https://i.sstatic.net/lpTrr.png My Ang ...

When you encounter an open response and need to resend it, simply click the "Send Again

After the discontinuation of Firebug, I find myself in need of two crucial functionalities that I used frequently. To replace these features, I am wondering if there are similar options available within the default Firefox Web Console. Previously, when ma ...

Display column header row divider for pinned columns in v5 DataGrid

I'm attempting to customize the appearance of pinned columns in MUI's DataGrid by adding a column header-row divider. The official demo by MUI for pinned columns [https://codesandbox.io/s/qix39o?file=/demo.tsx] displays the pinned column header ...

What are your thoughts on using a click function within the same tag as an ngFor directive in Angular and JavaScript?

There is a table with a row element called tr. Should the `Click` event use the same tag as the `*ngFor` directive? If this is not recommended, how can I enhance the code? ...

Can CSS be used to modify the size of the clickable region for links?

I'm currently working on a code block that resembles the following: <p class="cont"> <a href="otherpage.php" class="link"><img src="test.jpg" alt="" width="100" height="100"/></a> </p> Additionally, I have some CSS sty ...

Charting Add-Ons for jQuery

Looking for suggestions on user-friendly graph drawing plugins compatible with jQuery. Currently developing a web application that will retrieve data from a remote database and present it through visual graphs... Explored jgcharts (the jQuery Google Chart ...

Multiple charts experiencing disappearing x-axis labels on Flot

When trying to display two separate bar graphs, the x-axis labels seem to disappear. However, when only one graph is displayed, the labels show up perfectly fine. Any thoughts on why this might be happening? Here is the code snippet for displaying two gr ...

Why won't my Laravel AJAX request work after refreshing the page?

In my Laravel CRUD web application, users can view and edit records with a sorting feature that asynchronously sorts the main records view using AJAX. Everything functions smoothly until a user clicks on a record to edit it. After being redirected through ...

React Component: Issue with conditional "if else" statement not refreshing in return statement

Starting out in React and the front-end field with minimal experience. Currently attempting to dynamically change the color of the "fill" property in a polygon element using React. If the percentage is greater than 50, I want the color to be green; otherw ...

Encountering issues with the setSinkId() function in an Electron application

I'm currently working on an app that involves playing audio and allowing users to switch between audio devices. My main issue arises when attempting to utilize the setSinkId() function - it consistently results in a DOMException AbortError with the er ...

The Babel 5 plugin is encountering an issue due to an incompatible Babel version being used

Upon adding the relay-query plugin using yarn add -D babel-plugin-react-relay, and starting the development server, an error is encountered: Error: [BABEL] /Users/nemanja/sites/tictacturing/src/index.js: The (relay-query) Babel 5 plugin is being run with ...

executing certain code in a JavaScript file for Internet Explorer 9 compared to all other web browsers

As I analyze this single line of code, my goal is to create a conditional statement that executes different actions based on whether the browser is Internet Explorer 9 or not. Here's the code snippet: ('.fa-phone, .bg-darkPink').parent().on ...

Sending a POST request to a server using NodeJS with an array as the value for

Just to clarify, I am limited to using default modules in node version 9.x.x and cannot use the express module. I have a form where users can input basic information (name, birthday, email), and multiple user entries can be posted simultaneously. The serve ...

Having difficulty loading themes in AngularJS Material

I have added Angular (1) Material (v.1.1.5) to my Visual Studio project using NuGet and followed their instructions for referencing it: <script src="scripts/angular.min.js"></script> <script src="scripts/angular-route.min.js"></script ...