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

479
Visualizações
React-select is going blank when the options array changes (codesandbox included)

I am trying to use react-select in combination with match-sorter as described in this stackoverflow answer (their working version). I have an initial array of objects that get mapped to an array of objects with the value and label properties required by react-select, which is stored in state. That array is passed directly to react-select, and when you first click the search box everything looks good, all the options are there. The onInputChange prop is given a call to matchSorter, which in turn is given the array, the new input value, and the key the objects should be sorted on. In my project, and reproduced in the sandbox, as soon as you type anything into the input field, all the options disappear and are replaced by the no options message. If you click out of the box and back into it, the sorted options show up the way they should. See my sandbox for the issue, and here's the sandbox code:

import "./styles.css";
import { matchSorter } from "match-sorter";
import { useState } from "react";
import Select from "react-select";

const objs = [
  { name: "hello", id: 1 },
  { name: "world", id: 2 },
  { name: "stack", id: 3 },
  { name: "other", id: 4 },
  { name: "name", id: 5 }
];

const myMapper = (obj) => {
  return {
    value: obj.id,
    label: <div>{obj.name}</div>,
    name: obj.name
  };
};

export default function App() {
  const [options, setOptions] = useState(objs.map((obj) => myMapper(obj)));
  return (
    <Select
      options={options}
      onInputChange={(val) => {
        setOptions(matchSorter(options, val, { keys: ["name", "value"] }));
      }}
    />
  );
}

I am sure that the array in state is not getting removed or anything, I've console logged each step of the way and the array is definitely getting properly sorted by match-sorter. It's just that as soon as you type anything, react-select stops rendering any options until you click out and back in again. Does it have something to do with using JSX as the label value? I'm doing that in my project in order to display an image along with the options.

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

0

I had to do two things to make your code work:

  1. Replaced label: <div>{obj.name}</div> with label: obj.name in your mapper function.

    I am not sure if react-select allows html nodes as labels. Their documentation just defines it as type OptionType = { [string]: any } which is way too generic for anything.

  2. The list supplied to matchSorter for matching must be the full list (with all options). You were supplying the filtered list of previous match (from component's state).

const objs = [
  { name: "hello", id: 1 },
  { name: "world", id: 2 },
  { name: "stack", id: 3 },
  { name: "other", id: 4 },
  { name: "name", id: 5 }
];

const myMapper = (obj) => {
  return {
    value: obj.id,
    label: obj.name, // -------------------- (1)
    name: obj.name
  };
};

const allOptions = objs.map((obj) => myMapper(obj));

export default function App() {
  const [options, setOptions] = useState(allOptions);
  return (
    <Select
      options={options}
      onInputChange={(val) => {
        setOptions(
          matchSorter(
            allOptions, // ----------------> (2)
            val,
            { keys: ["name", "value"]
          }
        ));
      }}
    />
  );
}
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