Skip to content

Instantly share code, notes, and snippets.

@productioncoder
Created November 24, 2018 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save productioncoder/0c0a04c5534dfafc40539878499c7480 to your computer and use it in GitHub Desktop.
Save productioncoder/0c0a04c5534dfafc40539878499c7480 to your computer and use it in GitHub Desktop.
Youtube in React: fetching search results in lifecycle functions
/* ... */
class Search extends React.Component {
componentDidMount() {
if (!this.getSearchQuery()) {
// redirect to home component if search query is not there
this.props.history.push('/');
}
this.searchForVideos();
}
componentDidUpdate(prevProps) {
if (prevProps.youtubeApiLoaded !== this.props.youtubeApiLoaded) {
this.searchForVideos();
}
}
/* ... */
}
/* ... */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment