I've got a function which renders results from searching then filtering a seperate json file (not the issue) to map the results into each individual <div>
:
renderSearchResults() {
if (this.state.searchQuery.length > 0) {
const results = this.searchLocation(this.state.searchQuery);
if (results.length === 0) {
return 'No results were found. Try selecting from a list';
} else {
return results.slice(0, 10).map((result, i) => {
return (
<div className='trends--searchResultItem' key={i} onClick={() => alert('hjs')}>
{`${result.name}, ${result.country}`}
</div>
);
});
}
The onClick function should fire another function, but the alert() is there for testing, however this doesn't even work.
If I take away the parenthesis (onClick={alert('hjs')}
, then it fire's 1000s of times, and forces the application to crash, which suggest's im completely missing something in getting this to work.
If where the renderSearchResults()
function is makes a difference, it's in the render function in a class.
{
this.state.focused && (
<div className='trends--searchResults'>
{this.renderSearchResults()}
</div>
)
}
The state.focused is always set to true, so this isn't the issue either.
Thanks in advance!