Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
React-Select - handling changes for multiple selections and removals

I am using react-select and have the following setup below where a user is provided a report list of animal records with an "Edit" button against each record where they can assign/remove animal selections via this editable popup edit dialog.

On entry to this popup dialog, I have a useEffect hook that populates my animals array based on records edited and assigns this to the value prop within my react-select component. This is then shown correctly in my react-select select component.

At the moment, the user can see the label - "Dog".

Being a multi-select setup, my question is, when I try and select "Cat" and "Bird", my react-select component is not updating with these values and still only shows "Dog".

My question is, using the onChange, how I can reflect the new options selected within my select component as at the moment my handleChangeIndex function is not working as my optionsCopy console log is empty?

In this scenario, the end result that I am after is:

Animals assigned: Dog [x] Cat [x] Bird [x] where 'x' can be clicked to remove an entry.

const options = [
  { value: 0, label: "Dog" }, 
  { value: 1, label: 'Cat' },
  { value: 2, label: 'Bird' },
  { value: 3, label: 'Fish' }
]

const [animals, setAnimals] = useState([]);
const [selectedOptions, setSelectedOptions] = useState([]);

const handleChangeIndex = selectedOption => {
  const optionsCopy = [...selectedOptions];
  console.log("optionsCopy: ", optionsCopy)
  setSelectedOptions(optionsCopy);
};    


<Select 
   name="animal_names"
   options={options}
   isMulti={true}
   value={animals}
   onChange={(selectedOptions) => {
     handleChangeIndex(selectedOptions);
  }}                                              
/>          

 
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda