react-virtualized-select
HOC that uses react-virtualized and react-select to display large lists of options in a drop-down
Top Related Projects
The Select Component for React.js
React components for efficiently rendering large lists and tabular data
🤖 Headless UI for Virtualizing Large Element Lists in JS/TS, React, Solid, Vue and Svelte
React components for efficiently rendering large lists and tabular data
A tiny but mighty 3kb list virtualization library, with zero dependencies 💪 Supports variable heights/widths, sticky items, scrolling to index, and more!
The most powerful virtual list component for React
Quick Overview
React-virtualized-select is a React component that combines react-virtualized and react-select to create a dropdown menu with virtualized rendering. This allows for efficient handling of large datasets in dropdown menus, improving performance and user experience.
Pros
- Efficient rendering of large datasets in dropdown menus
- Seamless integration with React applications
- Customizable styling and behavior
- Supports both single and multi-select options
Cons
- Learning curve for developers unfamiliar with react-virtualized
- May be overkill for small datasets or simple dropdown needs
- Requires additional dependencies (react-virtualized and react-select)
- Limited documentation and examples for advanced use cases
Code Examples
- Basic usage with single select:
import VirtualizedSelect from 'react-virtualized-select';
const options = [
{ label: 'One', value: 1 },
{ label: 'Two', value: 2 },
// ... more options
];
function MyComponent() {
return (
<VirtualizedSelect
options={options}
onChange={(selectedOption) => console.log(selectedOption)}
/>
);
}
- Multi-select with custom styling:
import VirtualizedSelect from 'react-virtualized-select';
function MyMultiSelect() {
return (
<VirtualizedSelect
multi
options={options}
onChange={(selectedOptions) => console.log(selectedOptions)}
styles={{
control: (provided) => ({
...provided,
backgroundColor: '#f0f0f0',
borderColor: '#333',
}),
}}
/>
);
}
- Async loading of options:
import VirtualizedSelect from 'react-virtualized-select';
function AsyncSelect() {
const loadOptions = (inputValue) => {
return fetch(`/api/search?q=${inputValue}`)
.then((response) => response.json())
.then((data) => data.map((item) => ({ label: item.name, value: item.id })));
};
return (
<VirtualizedSelect
async
loadOptions={loadOptions}
onChange={(selectedOption) => console.log(selectedOption)}
/>
);
}
Getting Started
- Install the required packages:
npm install react-virtualized-select react-select react-virtualized
- Import and use the component in your React application:
import React from 'react';
import VirtualizedSelect from 'react-virtualized-select';
import 'react-virtualized-select/styles.css';
function App() {
const options = [
{ label: 'Option 1', value: 1 },
{ label: 'Option 2', value: 2 },
// ... more options
];
return (
<div>
<h1>My App</h1>
<VirtualizedSelect
options={options}
onChange={(selectedOption) => console.log(selectedOption)}
/>
</div>
);
}
export default App;
Competitor Comparisons
The Select Component for React.js
Pros of react-select
- More feature-rich with extensive customization options
- Larger community and more frequent updates
- Better documentation and examples
Cons of react-select
- Larger bundle size due to more features
- May have performance issues with large datasets
- Steeper learning curve for advanced customizations
Code Comparison
react-select:
import Select from 'react-select';
const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];
<Select options={options} />
react-virtualized-select:
import VirtualizedSelect from 'react-virtualized-select';
const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];
<VirtualizedSelect options={options} />
Both libraries offer similar basic usage, but react-virtualized-select is specifically designed for handling large datasets efficiently. react-select provides more built-in features and styling options out of the box, while react-virtualized-select focuses on performance optimization for long lists of options.
react-select is generally more suitable for projects requiring extensive customization and a wide range of features. react-virtualized-select is better for applications dealing with large datasets where performance is a primary concern.
React components for efficiently rendering large lists and tabular data
Pros of react-window
- Smaller bundle size and improved performance
- Simpler API with fewer components
- Better support for dynamic content and variable sizes
Cons of react-window
- Less feature-rich compared to react-virtualized-select
- Requires more manual configuration for complex use cases
- Limited built-in support for advanced features like multi-column layouts
Code Comparison
react-virtualized-select:
import { List } from 'react-virtualized';
<List
width={300}
height={300}
rowCount={1000}
rowHeight={30}
rowRenderer={({ index, key, style }) => (
<div key={key} style={style}>
Row {index}
</div>
)}
/>
react-window:
import { FixedSizeList } from 'react-window';
<FixedSizeList
height={300}
itemCount={1000}
itemSize={30}
width={300}
>
{({ index, style }) => (
<div style={style}>Row {index}</div>
)}
</FixedSizeList>
react-window offers a more streamlined API with improved performance, making it ideal for simpler use cases and projects prioritizing efficiency. However, react-virtualized-select provides more built-in features and flexibility for complex scenarios, albeit with a larger bundle size and potentially slower performance. The choice between the two depends on the specific requirements of your project and the level of customization needed.
🤖 Headless UI for Virtualizing Large Element Lists in JS/TS, React, Solid, Vue and Svelte
Pros of Virtual
- Framework-agnostic, supporting React, Vue, Solid, and more
- More flexible and customizable, allowing for complex virtualization scenarios
- Active development and maintenance, with frequent updates and improvements
Cons of Virtual
- Steeper learning curve due to its more generic and flexible nature
- Requires more setup and configuration compared to the simpler React-specific solution
Code Comparison
React-virtualized-select:
import VirtualizedSelect from 'react-virtualized-select';
<VirtualizedSelect
options={options}
onChange={selectValue}
value={selectedValue}
/>
Virtual:
import { useVirtualizer } from '@tanstack/react-virtual';
const rowVirtualizer = useVirtualizer({
count: rows.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 35,
});
{rowVirtualizer.getVirtualItems().map((virtualRow) => (
<div key={virtualRow.index}>{rows[virtualRow.index]}</div>
))}
Summary
Virtual offers more flexibility and framework support but requires more setup. React-virtualized-select provides a simpler, React-specific solution for virtualized select components. Choose based on your project's needs and complexity requirements.
React components for efficiently rendering large lists and tabular data
Pros of react-virtualized
- More comprehensive solution for virtualizing various UI components
- Offers a wider range of components (Grid, List, Table, etc.)
- Better performance for large datasets and complex layouts
Cons of react-virtualized
- Steeper learning curve due to more complex API
- Potentially overkill for simpler use cases
- Requires more setup and configuration
Code Comparison
react-virtualized:
import { List } from 'react-virtualized';
<List
width={300}
height={300}
rowCount={1000}
rowHeight={20}
rowRenderer={({ index, key, style }) => (
<div key={key} style={style}>
Row {index}
</div>
)}
/>
react-virtualized-select:
import VirtualizedSelect from 'react-virtualized-select';
<VirtualizedSelect
options={[...]}
onChange={(selectValue) => this.setState({ selectValue })}
value={this.state.selectValue}
/>
react-virtualized provides a more flexible and powerful solution for virtualizing various UI components, while react-virtualized-select focuses specifically on enhancing select inputs with virtualization. The former offers better performance and a wider range of components but comes with a steeper learning curve. react-virtualized-select is simpler to use but limited in scope. Choose based on your specific needs and project complexity.
A tiny but mighty 3kb list virtualization library, with zero dependencies 💪 Supports variable heights/widths, sticky items, scrolling to index, and more!
Pros of react-tiny-virtual-list
- Lightweight and focused on a single purpose (virtualization)
- Simple API, easy to integrate into existing projects
- Supports both fixed and variable height items
Cons of react-tiny-virtual-list
- Less feature-rich compared to react-virtualized-select
- Doesn't include built-in select functionality
- May require additional components for complex use cases
Code Comparison
react-tiny-virtual-list:
import VirtualList from 'react-tiny-virtual-list';
<VirtualList
width='100%'
height={600}
itemCount={items.length}
itemSize={50}
renderItem={({index, style}) => (
<div key={index} style={style}>{items[index]}</div>
)}
/>
react-virtualized-select:
import VirtualizedSelect from 'react-virtualized-select';
<VirtualizedSelect
options={options}
onChange={(selectValue) => this.setState({ selectValue })}
value={this.state.selectValue}
/>
The code comparison shows that react-tiny-virtual-list is more focused on rendering a virtualized list of items, while react-virtualized-select provides a complete select component with virtualization built-in. react-tiny-virtual-list requires more manual setup for rendering items, but offers more flexibility in terms of what can be rendered. react-virtualized-select, on the other hand, provides a more opinionated and feature-complete solution for creating virtualized select components.
The most powerful virtual list component for React
Pros of react-virtuoso
- More modern and actively maintained, with frequent updates
- Supports both fixed and variable size items out of the box
- Offers a simpler API with less configuration required
Cons of react-virtuoso
- Less mature ecosystem and community support
- Fewer specialized components (e.g., no built-in select component)
- May require more custom implementation for complex use cases
Code Comparison
react-virtualized-select:
import VirtualizedSelect from 'react-virtualized-select';
<VirtualizedSelect
options={options}
onChange={selectValue}
value={selectedValue}
/>
react-virtuoso:
import { Virtuoso } from 'react-virtuoso';
<Virtuoso
style={{ height: '400px' }}
totalCount={1000}
itemContent={index => <div>Item {index}</div>}
/>
While react-virtualized-select provides a ready-to-use select component with virtualization, react-virtuoso offers a more generic virtualization solution that can be adapted for various use cases, including select components with some additional implementation.
react-virtuoso focuses on providing a flexible and performant virtualization library, while react-virtualized-select is specifically tailored for select components. The choice between the two depends on the specific requirements of your project and the level of customization needed.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
This component is no longer supported
Hello! This package is built on react-virtualized (a library that I no longer support) and vesion 1.0 of react-select (which is no longer the current version). As such, I've decided to stop supporting this package. GitHub issues and pull requests may be ignored.
If you are interested in taking over maintenance of this package, please send me an email (see my GitHub profile) and I'd be happy to add you as a collaborator.
React Virtualized Select
Demos available here: http://bvaughn.github.io/react-virtualized-select/
Getting started
Install react-virtualized-select
using npm.
npm install react-virtualized-select --save
ES6, CommonJS, and UMD builds are available with each distribution. For example:
// Make sure to import default styles.
// This only needs to be done once; probably during bootstrapping process.
import 'react-select/dist/react-select.css'
import 'react-virtualized-select/styles.css'
// Then import the virtualized Select HOC
import VirtualizedSelect from 'react-virtualized-select'
Alternately you can load a global-friendly UMD build:
<link rel="stylesheet" href="path-to-react-select/dist/react-select.css">
<link rel="stylesheet" href="path-to-react-virtualized/styles.css">
<link rel="stylesheet" href="path-to-react-virtualized-select/styles.css">
<script src="path-to-react-virtualized-select/dist/umd/react-virtualized-select.js"></script>
Example
react-select-virtualized works just like react-select. You pass it an array of options, along with almost any other parameters supported by the Select
component.
Try this example in Code Sandbox.
// Import default styles.
// This only needs to be done once; probably during bootstrapping process.
import "react-select/dist/react-select.css";
import "react-virtualized-select/styles.css";
import React from "react";
import ReactDOM from "react-dom";
import Select from "react-virtualized-select";
// Dummy array of test values.
const options = Array.from(new Array(1000), (_, index) => ({
label: `Item ${index}`,
value: index
}));
ReactDOM.render(
<Select options={options} />,
document.getElementById("root")
);
React Virtualized Select Props
The additional parameters introduced by react-select-virtualized are optional. They are:
Property | Type | Description |
---|---|---|
async | PropTypes.bool | Use Select.Async internally; if this property is specified then a loadOptions method should also be used. |
maxHeight | PropTypes.number | Max height of options menu; defaults to 200 pixels. |
optionHeight | PropTypes.number or PropTypes.func | Option height (defaults to 35 pixels). Dynamic height can be supported via a function with the signature ({ option: Object }): number |
optionRenderer | PropTypes.func | Custom option renderer; (see below for signature). |
selectComponent | PropTypes.func | Use a specific select HOC (eg Select , Select.Creatable , Select.Async or Select.AsyncCreatable ); defaults to Select (or Select.Async if async flag is true). |
Unsupported props
optionComponent
is not supported for react-select-virtualized; optionRenderer
must be used instead, see below for usage.
Custom Option Renderer
You can override the built-in option renderer by specifying your own optionRenderer
property. Your renderer should return a React element that represents the specified option. It will be passed the following named parameters:
Property | Type | Description |
---|---|---|
focusedOption | Object | The option currently-focused in the dropdown. Use this property to determine if your rendered option should be highlighted or styled differently. |
focusedOptionIndex | number | Index of the currently-focused option. |
focusOption | Function | Callback to update the focused option; for example, you may want to call this function on mouse-over. |
key | string | A unique identifier for each element created by the renderer. |
labelKey | string | Attribute of option that contains the display text. |
option | Object | The option to be rendered. |
options | Array<Object> | Array of options (objects) contained in the select menu. |
selectValue | Function | Callback to update the selected values; for example, you may want to call this function on click. |
style | Object | Styles that must be passed to the rendered option. These styles are specifying the position of each option (required for correct option displaying in the dropdown). |
valueArray | Array<Object> | Array of the currently-selected options. Use this property to determine if your rendered option should be highlighted or styled differently. |
valueKey | string | Attribute of option that contains the value. |
optionRenderer example
It should be noted that in order to successfully set the active index in your custom renderer, you need to call the selectValue
prop. A common pattern is to bind onto your onClick
handler in your custom element. The example that follows also provides the required style
prop (as noted above), which is necessary to properly position the element. Refer to the full example for the complete usage.
function Option({
style,
option,
selectValue,
}) {
return (
<a
style={style}
onClick={() => selectValue(option)}
>
{option.value}
</a>
);
}
Top Related Projects
The Select Component for React.js
React components for efficiently rendering large lists and tabular data
🤖 Headless UI for Virtualizing Large Element Lists in JS/TS, React, Solid, Vue and Svelte
React components for efficiently rendering large lists and tabular data
A tiny but mighty 3kb list virtualization library, with zero dependencies 💪 Supports variable heights/widths, sticky items, scrolling to index, and more!
The most powerful virtual list component for React
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot