在可搜索的react select下拉列表中呈现大型数据列表的最佳方式是什么?我研究过窗口,在窗口中,只为在视口中可见的项目创建DOM节点。这可以通过使用react-window包和react-select来完成。然而,我想知道是否有比这更好的方法。
这是使用react-window和react-select的窗口实现
import React, { Component } from "react";
import ReactDOM from "react-dom";
import Select from "react-select";
import { FixedSizeList as List } from "react-window";
import "./styles.css";
const options = [];
for (let i = 0; i < 10000; i = i + 1) {
options.push({ value: i, label: `Option ${i}` });
}
const height = 35;
class MenuList extends Component {
render() {
const { options, children, maxHeight, getValue } = this.props;
const [value] = getValue();
const initialOffset = options.indexOf(value) * height;
return (
<List
height={maxHeight}
itemCount={children.length}
itemSize={height}
initialScrollOffset={initialOffset}
>
{({ index, style }) => <div style={style}>{children[index]}</div>}
</List>
);
}
}
const App = () => <Select components={{ MenuList }} options={options} />;
ReactDOM.render(<App />, document.getElementById("root"));发布于 2021-09-09 04:01:23
我将您提到的解决方案(react-window)与filterOption解决方案以及较少讨论的react-async组件组合在一起。这对我来说效果很好。
react-window将执行某种“惰性加载”,而异步react会在显示搜索查询之前显示一个加载标志。这些结合在一起让它感觉更自然(我有7000+选项)。
这是我第一次回复帖子,如果有人有问题,请告诉我,我会尽我所能回答
import React, { Component } from "react";
import AsyncSelect from "react-select/async";
import { FixedSizeList as List } from "react-window";
import { createFilter } from "react-select";
import "./styles.css";
const TestSelect = (vendorOptions) => {
const options = [];
for (let i = 0; i < vendorOptions["vendorOptions"].length; i = i + 1) {
options.push({ value: vendorOptions["vendorOptions"][i], label: `${vendorOptions["vendorOptions"][i]}` });
}
const loadOptions = (inputValue, callback) => {
setTimeout(() => {
callback(options);
}, 1000);
};
const height = 35;
class MenuList extends Component {
render() {
const { options, children, maxHeight, getValue } = this.props;
const [value] = getValue();
const initialOffset = options.indexOf(value) * height;
return (
<List
height={maxHeight}
itemCount={children.length}
itemSize={height}
initialScrollOffset={initialOffset}
>
{({ index, style }) => <div style={style}>{children[index]}</div>}
</List>
);
}
}
const TestSelectComponent = () => {
return(
<div className ="testDropdown">
<AsyncSelect components={{ MenuList }} cacheOptions defaultOptions loadOptions={loadOptions} filterOption={createFilter({ ignoreAccents: false })}/>
</div>
)
}
return (
<TestSelectComponent />
)
}
export default TestSelect
发布于 2020-09-15 17:39:04
如果你看一下,你会发现默认值是ignoreAccents: true。此缺省值使react-select两次调用名为stripDiacritics的代价高昂的函数。这会导致性能问题。
在react-select中包含ignoreAccents: false。
示例:filterOption={createFilter({ ignoreAccents: false })}
https://stackoverflow.com/questions/62143009
复制相似问题