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

256
Visualizações
onChange react-select multiple options

I need to detect if I tipe into a searchable react-select. According to the official documentation first of all I have to pass isMulti prop in order to select more than an option.

The actual built-in onChange detects changes only when I select an option, and not when I type into the box. I need it because I have to call an API if I type into that input box.

Here is My select:

  <Select
    options={myOptions}
    isMulti
    loadingIndicator={true}
    styles={selectStyle}
    onChange={(e) => console.log(`e`, e)}
  />

Actually onChange logs me an array with the options selected. How can I change that for example if I type "fo" then I have "fo"? Is there any option or way to do so?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Yes, we have async select option to load data from API when user type. Below I have attached the code for you documented in their official website. https://react-select.com/home#async

import { useState } from "react";
import AsyncSelect from "react-select/async";

const colourOptions = [
  { label: "Oranage", value: "orange" },
  { label: "Yellow", value: "yellow" },
  { label: "Blue", value: "blue" }
];

const SelectOption = () => {
  const [inputValue, setInputValue] = useState("");

  const filterColors = (inputValue) => {
    return colourOptions.filter((i) =>
      i.label.toLowerCase().includes(inputValue.toLowerCase())
    );
  };

  const promiseOptions = (inputValue, callback) => {
    setTimeout(() => {
      callback(filterColors(inputValue));
    }, 1000);
  };

  const handleChange = (newValue) => {
    const inputValue = newValue.replace(/\W/g, "");
    setInputValue(inputValue);
    return inputValue;
  };

  return (
    <AsyncSelect
      cacheOptions
      defaultOptions
      loadOptions={promiseOptions}
      onInputChange={handleChange}
    />
  );
};

export default SelectOption;
over 4 years ago · Santiago Trujillo Relatório

0

There is no onKeyDown props available in react-select's documentation.

What you can do is something like below:

class App extends Component {

  onKeyUp = () => {
    console.log("I see");
    // Your rest code
  };

  render() {
    return (
      <div onKeyUp={this.onKeyUp} className="App">
          <Select
          options={myOptions}
          isMulti
          loadingIndicator={true}
          styles={selectStyle}
          onChange={(e) => console.log(`e`, e)}
          />
      </div>
    );
  }
}
over 4 years ago · Santiago Trujillo Relatório
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