Last active
January 13, 2019 09:45
-
-
Save msojda/1bcba6f5d4e2e39a9abb26880119d663 to your computer and use it in GitHub Desktop.
React hooks App example
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, Fragment } from 'react'; | |
import * as client from './OpenLibraryClient'; | |
import BooksList from './BooksList'; | |
import SearchForm from './SearchForm'; | |
class App extends Component { | |
state = { books: [], isFetching: false, query: '', numFound: 0 }; | |
onSearch = async e => { | |
e.preventDefault(); | |
this.setState({ isFetching: true, books: [] }); | |
const result = await client.findBooks(this.state.query); | |
const { docs = [], numFound = 0 } = result; | |
this.setState({ books: docs, isFetching: false, numFound }); | |
}; | |
onQueryChange = ({ target: { value } }) => { | |
this.setState({ query: value }); | |
}; | |
render() { | |
return ( | |
<Fragment> | |
<section className="section"> | |
<div className="container"> | |
<h1 className="title has-text-centered"> | |
Open Library books search | |
</h1> | |
</div> | |
</section> | |
<SearchForm | |
onQueryChange={this.onQueryChange} | |
onSearch={this.onSearch} | |
query={this.state.query} | |
/> | |
<BooksList | |
loading={this.state.isFetching} | |
books={this.state.books} | |
count={this.state.numFound} | |
/> | |
</Fragment> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment