Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
React-select multiple selects on one page

I am a bit confused, here is an example with a couple of select inputs that have the same state, please check here: https://stackblitz.com/edit/get-selected-by-value-multi-select-react-agamk4?file=src/App.js so please:

  • How can I make it so when I select an option the value does not apply to the rest of the select inputs?
  • How would you put the values in the store for each of the selects?
  • Do I need multiple stores?

For more clarity, here is a screenshot: https://www.awesomescreenshot.com/image/19798040?key=bb839c650c93b436066e03d33d5515b0 I hope this makes sense? What would be the best approach? Thank you.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I have shared the code in case of only a single state. You can use this method if you want only a single state but having multiple states for different select inputs also won't be bad as you have only 3 inputs. Having single state method would be useful if number of select inputs would have more.

import React, { useState } from 'react';
import Select from 'react-select';

function App() {
  const data = [
    {
      value: 1,
      label: 'cerulean',
    },
    {
      value: 2,
      label: 'fuchsia rose',
    },
    {
      value: 3,
      label: 'true red',
    },
    {
      value: 4,
      label: 'aqua sky',
    },
    {
      value: 5,
      label: 'tigerlily',
    },
    {
      value: 6,
      label: 'blue turquoise',
    },
  ];

  // set value for default selection
  const [selectedValue, setSelectedValue] = useState([
    { value: [] },
    { value: [] },
    { value: [] },
  ]);

  // handle onChange event of the dropdown
  const handleChange = (e, no) => {
    setSelectedValue(
      selectedValue.map((item) => {
        return selectedValue.indexOf(item) === no
          ? { value: Array.isArray(e) ? e.map((x) => x.value) : [] }
          : item;
      })
    );
  };

  return (
    <div className="App">
      <Select
        className="dropdown"
        placeholder="Select Option"
        value={data.filter((obj) => selectedValue[0].value.includes(obj.value))} // set selected values
        options={data} // set list of the data
        onChange={(event) => handleChange(event, 0)} // assign onChange function
        isMulti
        isClearable
      />
      <br />
      <Select
        className="dropdown"
        placeholder="Select Option"
        value={data.filter((obj) => selectedValue[1].value.includes(obj.value))} // set selected values
        options={data} // set list of the data
        onChange={(event) => handleChange(event, 1)} // assign onChange function
        isMulti
        isClearable
      />
      <br />
      <Select
        className="dropdown"
        placeholder="Select Option"
        value={data.filter((obj) => selectedValue[2].value.includes(obj.value))} // set selected values
        options={data} // set list of the data
        onChange={(event) => handleChange(event, 2)} // assign onChange function
        isMulti
        isClearable
      />

      {selectedValue && (
        <div style={{ marginTop: 20, lineHeight: '25px' }}>
          <div>
            <b>Selected Value: </b> {JSON.stringify(selectedValue, null, 2)}
          </div>
        </div>
      )}
    </div>
  );
}

export default App;


about 4 years ago · Juan Pablo Isaza Denunciar

0

{selectedValue && (
        <div style={{ marginTop: 20, lineHeight: '25px' }}>
          <div>
            <b>Selected Values: </b>
            <span>{
              selectedValue.map(item => item.value.length !== 0 ? 
                <li>{data.filter(data => data.value === item.value[0])[0].label}</li> :
                <li>No value selected</li>
                )
              }</span>
          </div>
        </div>
      )}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda