Last active
January 26, 2020 21:12
-
-
Save FrankGerold/f496400bb616497837cbd2e9726ce41d to your computer and use it in GitHub Desktop.
Book Search Class
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
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