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

242
Visualizações
Getting back the value of a `cleared` input element with React Select

I have a several ReactSelect components, and one piece of global state which is responsible for holding all my selected values in an array.

Select

      <Select
        styles={inputStyles}
        className="basic-single"
        classNamePrefix="select"
        isClearable={true}
        isSearchable={false}
        placeholder={'Select Your Most Convenient Time Slot'}
        options={newHoursArr}
        isMulti={false}
        onChange={(values) => handleChange(values)}

        defaultValue={clientServiceReferral.selectedTimeSlots.map((referral) => (
          referral.timeSlot === timeWithDate ? (
            { ['label']: referral.value, ['value']: referral.value }
          ) : null
        ))}
      />

handleChange function

  const handleChange = (value) => {

    const found = clientServiceReferral.selectedTimeSlots.find(element => element.timeSlot === timeWithDate);

    if (found) {
      clientServiceReferral.selectedTimeSlots.splice(found, 1)
    }

    const newValue = {
      timeSlot: timeWithDate,
      value: value.value
    }

    setClientServiceReferral({
      ...clientServiceReferral,
      selectedTimeSlots: [...clientServiceReferral.selectedTimeSlots, newValue]
    })
  }

ReactSelect has an isClearable prop. Which allows users to clear the input with a button click. This returns a value of null when values is logged in the onChange function, but is there a way to return the actual value inside the select that is getting cleared when the clear button is clicked?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There's an optional second parameter passed to the onChange event. It's of this type:

export interface ActionMetaBase<Option> {
  option?: Option | undefined;
  removedValue?: Option;
  removedValues?: Options<Option>;
  name?: string;
}

Now, I've never used this library, but it looks like removedValue or removedValues could be helpful? idk.

Anyway, I got that from their docs. Hope it works out for you:

about 4 years ago · Juan Pablo Isaza Relatório

0

For anyone interested, Via Joshua Wood's answer, the value of any cleared item(s) can be found as so:

onChange={(values, removedValue) => handleChange(values, removedValue)}
  const handleChange = (value, removedValue) => {

    if (removedValue.action === 'clear') {
      console.log('removed', removedValue.removedValues[0])
    }
// removedValues returns an array
about 4 years ago · Juan Pablo Isaza 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