Last active
May 23, 2019 04:38
-
-
Save leofab86/09a9d539dda34506c37a9ad74487404b to your computer and use it in GitHub Desktop.
Fuzzy search autocomplete v1.2.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//searchResults.js | |
export default class SearchResults extends React.Component { | |
constructor (props) { | |
super(); | |
this.state = { | |
searchResults: [], | |
} | |
//initiate the webworker: | |
this.webWorker = new Worker('...path to webWorker.js') | |
//pass it the 13,000 item search data to initialize the searchEngine with: | |
this.webWorker.postMessage({data: props.data}) | |
//assign the handler that will accept the searchResults when it sends them back: | |
this.webWorker.onmessage = this.handleResults | |
} | |
componentDidUpdate(prevProps) { | |
const {searchTerm} = this.props; | |
if(searchTerm && searchTerm !== prevProps.searchTerm) { | |
//change our async search request to a .postMessage, the messaging API of webWorkers: | |
this.webWorker.postMessage({searchTerm}) | |
} | |
} | |
handleResults = (e) => { | |
const {searchResults} = e.data | |
this.setState({ | |
searchResults | |
}) | |
} | |
render () { | |
return <ReactVirtualizedList searchResults={this.state.searchResults}/> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment