Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

484
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!