Skip to content

Instantly share code, notes, and snippets.

@FrankGerold
Last active January 26, 2020 21:12
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 FrankGerold/f496400bb616497837cbd2e9726ce41d to your computer and use it in GitHub Desktop.
Save FrankGerold/f496400bb616497837cbd2e9726ce41d to your computer and use it in GitHub Desktop.
Book Search Class
import React, { Component } from 'react';
class BookSearch extends Component {
//Empty list initializing state
state = {
bookList: []
}
// Perform external fetch after mounting component
componentDidMount() {
fetch('https://www.googleapis.com/books/v1/volumes?q=react+JS+hooks')
.then(response => response.json())
.then((searchResults) => {
this.setState({
//Set book list in state to list of search result items
bookList: searchResults.items
})
})
}
render() {
return (
<div>
//Map out an unordered list of each search result's title
<ul>
{this.state.bookList.map((book) => {
return <li>{book.volumeInfo.title}</li>
})}
</ul>
</div>
);
}
}
export default BookSearch;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment