question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[v2] react-select didn't work for long lists

See original GitHub issue

Hi there

I’m testing out react-select v2 and I noticed performance issues once when I have a long lists of 300,000 items. I wanted to know if there is any interest to build in something like react-virtualized into react-select so users can scroll through long lists without jank or is this out of scope of this project.

Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:37 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
Janpotcommented, Nov 28, 2018

Another small issue with @tmacdonald solution is that the menu doesn’t shrink when there’s small amount of options when typing in the search box.

PS: I slightly modified the solution with VariableSizeList to account for option groups

import React from 'react';
import Select from 'react-select';
import { VariableSizeList as List } from 'react-window';

const GROUP_HEADER_HEIGHT = 13;
const ITEM_HEIGHT = 34;

function MenuList (props) {
  const { options, getValue } = props;
  const [ value ] = getValue();

  const initialOffset = options.indexOf(value) * ITEM_HEIGHT;

  const children = React.Children.toArray(props.children);

  function getOptionSize (option) {
    if (option.options) {
      return option.options.length * ITEM_HEIGHT + GROUP_HEADER_HEIGHT;
    }
    return ITEM_HEIGHT;
  }

  function getItemSize (i) {
    return getOptionSize(options[i]);
  }

  const totalHeight = options.reduce((height, option) => {
    return height + getOptionSize(option);
  }, 0);

  const estimatedItemSize = totalHeight / options.length;

  return (
    <List
      height={Math.min(totalHeight, 300)}
      itemCount={children.length}
      itemSize={getItemSize}
      estimatedItemSize={estimatedItemSize}
      initialScrollOffset={initialOffset}
    >
      {({ index, style }) => <div style={style}>{children[index]}</div>}
    </List>
  );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

React-Select not showing options - Stack Overflow
If I inspect the select component the options prop is filled, but the options aren't being displayed. I've tried both an array of...
Read more >
Styles - React Select
Styles. React Select offers 3 main APIs for styling: The styles prop; The classNames prop; The classNamePrefix prop ...
Read more >
React key attribute: best practices for performant lists
Looking into how React "key" attribute works, how to use it correctly, how to improve performance of lists with it, and why array...
Read more >
Programmatically Add New Options To Dropdown ... - YouTube
In this video, I will show you how to programmatically add a new item to the select list using react select creatable. React...
Read more >
React Select Components Library - OnAirCode
Your application deserves a good select option with react selectors ... 2. React-Select. So we didn't have to wait too long for selection ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found