Created
September 27, 2018 19:26
-
-
Save yeti-detective/81385d599356e3e4209de2e6528e24d5 to your computer and use it in GitHub Desktop.
Method of a React web component that displays search results
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
// this is one method of a component from my SoundClone app | |
buildSearchPool () { | |
if (this.state.query === '') { | |
this.setState({ | |
searchPool: [] | |
}) | |
} else { | |
const pool = []; | |
Object.keys(this.props.songs).filter((songId) => { | |
const song = this.props.songs[songId]; | |
if (song.title.toUpperCase().includes(this.state.query.toUpperCase())) { | |
pool.push({ | |
name: song.title, | |
img_url: song.image_url, | |
url: `/users/${song.user_id}/${song.id}`, | |
type: 'song' | |
}) | |
}; | |
}) | |
Object.keys(this.props.users).forEach((userId) => { | |
const user = this.props.users[userId]; | |
if (user.username.toUpperCase().includes(this.state.query.toUpperCase())) { | |
pool.push({ | |
name: user.username, | |
img_url: user.icon_url, | |
url: `/users/${user.id}`, | |
type: 'user' | |
}); | |
} | |
}) | |
this.setState({ | |
searchPool: pool | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment