English 中文(简体)
使用非常大选项列表的反应选择
原标题:React Select with very large options lists
I have a select component that will need to handle around 7,000 options in it. I am running into two problems. 1) when typing into the search parameter things are loading too slowly. 2) I need to filter though all of the options and disable options that have previously been selected (from values I load from the database) or have just been selected on this page load. For problem number 1 I have tried to leverage https://github.com/bvaughn/react-select-fast-filter-options and it works on first page load. I run into issues whenever I try to modify the options in any way, as you will see I originally try to load in the options via an ajax call (which I can change) or if I need to disable options dynamically I think that may break it. For problem number 2, when I try to filter though all of these options, it takes a good long time because I am cycling though all 7,000 options each time a person makes a selection in the list. Some guidance on this may be helpful. For further context here is the code I have so far: import React, {Component} from react import PropTypes from prop-types ; import react-select/dist/react-select.css import react-virtualized/styles.css import react-virtualized-select/styles.css import VirtualizedSelect from react-virtualized-select import axios from axios ; class StockSearch extends Component { static propTypes = { exchanges: PropTypes.array.isRequired, onSelectChange: PropTypes.func.isRequired, searchDisabled: PropTypes.bool.isRequired, picks: PropTypes.array.isRequired, stock_edit_to_show: PropTypes.number } state = { stocks: [], selected: [] } componentWillReceiveProps = (nextProps) => { } /** * Component Bridge Function * @param stock_id stocks id in the database */ stockSearchChange = (stock_id) => { this.props.onSelectChange(stock_id); } componentWillMount = () => { this.fetchStocks(this.props.exchanges); } /** * Responsible for fetching all of the stocks in the database * @param exchanges comma denominated list of exchange ids */ fetchStocks = (exchanges) => { let stringExchanges = exchanges.join(); axios.get( /stock-search-data-by-exchange/ , { params: { exchanges: stringExchanges } }) .then(response => { this.setState({ stocks: response.data }) }) .catch(error => { console.log(error); }) } /** * handles selected option from the stock select * @param selectedOption */ handleSelect = (selectedOption) => { this.stockSearchChange(selectedOption.value); } render() { return (
) } } export default StockSearch;
问题回答
For problem #1, react-windowed-select has been very useful for me in a similar circumstance: https://github.com/jacobworrel/react-windowed-select For problem #2, I have found that react-windowed-select redraws extremely quickly. You can experiment with filters with your dataset, here is a code snippet to get you started: const startTime = Date.now() // Create a 7000 element array with a bunch of content, in this case junk strings array = [...Array(7000)].map(i => Math.random().toString(36).replace(/[^a-z]+/g, ).substr(0, 5)) const arrayBuiltTime = Date.now() // Filter out any string with the letter q to emulate a filtering operation const filteredArray = array.filter(i => !i.includes( q ) ) const doneTime = Date.now() // See how long it takes :-) console.log(startTime) console.log(arrayBuiltTime) console.log(doneTime) https://codepen.io/smeckman/pen/zYOrjJa?editors=1111




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签