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
render () { | |
return ( | |
<Autocomplete | |
value={this.state.searchTerm} | |
items={this.getSearchResults()} | |
renderMenu={this.reactVirtualizedList} | |
renderItem={this.renderItem} | |
getItemValue={ item => item.name } | |
onChange={(e, value) => this.setState({searchTerm: value})} | |
/> |
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
getSearchResults = () => { | |
const {searchTerm} = this.state; | |
return searchTerm ? this.searchEngine.search(searchTerm) : [] | |
// searchEngine.search is the expensive search algorithm | |
}; |
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
//autocomplete.js | |
render () { | |
return ( | |
<div> | |
<input | |
onChange={ e => this.setState({searchTerm: e.target.value})} | |
value={this.state.searchTerm}/> | |
<SearchResults |
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 | |
componentDidUpdate(prevProps) { | |
const {searchTerm, searchEngine} = this.props; | |
if(searchTerm && searchTerm !== prevProps.searchTerm) { | |
this.setState({ | |
searchResults: searchEngine.search(searchTerm) | |
}) | |
} | |
} |
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 | |
componentDidUpdate(prevProps) { | |
const {searchTerm, searchEngine} = this.props; | |
if(searchTerm && searchTerm !== prevProps.searchTerm) { | |
/* stick the update with the expensive search method into | |
a promise callback: */ | |
Promise.resolve().then(() => { | |
this.setState({ | |
searchResults: searchEngine.search(searchTerm) |
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 | |
componentDidUpdate(prevProps) { | |
const {searchTerm, searchEngine} = this.props; | |
if(searchTerm && searchTerm !== prevProps.searchTerm) { | |
/* stick the update with the expensive search method into | |
a setTimeout callback: */ | |
const setTimeoutCallback = () => { | |
this.setState({ | |
searchResults: searchEngine.search(searchTerm) |
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
//autocomplete.js | |
render () { | |
return ( | |
<div> | |
<input | |
onChange={ e => this.setState({searchTerm: e.target.value})} | |
value={this.state.searchTerm}/> | |
<SearchResults | |
searchEngine={this.props.searchEngine} |
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
app.route('/prep-staging/testSearch') | |
.post(bodyParser.json(), (req, res) => { | |
const {data, searchTerm} = req.body | |
const engine = new SearchEngine(data) | |
const searchResults = engine.search(searchTerm) | |
res.send({searchResult}) |
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 | |
componentDidUpdate(prevProps) { | |
const {searchTerm, searchEngine, data} = this.props; | |
if(searchTerm && searchTerm !== prevProps.searchTerm) { | |
/* ping the search route with the searchTerm and update state | |
with the results when they return: */ | |
fetch(`testSearch`, { | |
method: 'POST', | |
body: JSON.stringify({data, searchTerm}), |
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') |
OlderNewer