Skip to content

Instantly share code, notes, and snippets.

@msojda
Last active January 13, 2019 09:45
Show Gist options
  • Save msojda/1bcba6f5d4e2e39a9abb26880119d663 to your computer and use it in GitHub Desktop.
Save msojda/1bcba6f5d4e2e39a9abb26880119d663 to your computer and use it in GitHub Desktop.
React hooks App example
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