Welcome folks today in this blog post we will be building a react.js book search
app using google books api
using typescript. All the full source code of the application is shown below.
Get Started
In order to get started you need to copy paste the below code inside the App.js
file as shown below
App.js
1 2 3 4 5 6 7 8 9 10 11 12 |
import React from 'react' import './App.css'; import Country from './components/country'; import GoogleBooksSearch from "./googleBooksSearch"; function App() { return ( <GoogleBooksSearch></GoogleBooksSearch> ) } export default App; |
And now we need to build this bookSearch
component as shown below
googleBooksSearch.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import React, { useState } from "react"; import axios from 'axios'; import { Card } from 'react-bootstrap'; function googleBooksSearch() { const [book, setBook] = useState(""); const [result, setResult] = useState([]); const [apiKey, setApiKey] = useState("AIzaSyCqi37mzRrzkBrDZDb0BX9_IarX5iMOT88") function handleChange(event) { const book = event.target.value; setBook(book); } function handleSubmit(event) { event.preventDefault(); axios.get("https://www.googleapis.com/books/v1/volumes?q=" + book + "&key=" + apiKey + "&maxResults=40") .then(data => { console.log(data.data.items); setResult(data.data.items); }) } return ( <form onSubmit={handleSubmit}> <div className="card-header main-search"> <div className="row"> <div className="col-12 col-md-3 col-xl-3"> <input onChange={handleChange} className="AutoFocus form-control" placeholder="Type something..." type="text" /> </div> <div className="ml-auto"> <input type="submit" value="Search" className="btn btn-primary search-btn" /> </div> </div> </div> <div className="container"> <div className="row"> {result.map(book => ( <div className="col-sm-2"> <Card style={{ 'marginTop': '10px' }}> <Card.Img variant="top" src={book.volumeInfo.imageLinks !== undefined ? book.volumeInfo.imageLinks.thumbnail : ''} alt={book.title} /> <Card.Body> <h5 className="card-title">Card title</h5> <a className="btn btn-primary">Know more</a> </Card.Body> </Card> </div> ))} </div> </div> </form> ) } export default googleBooksSearch |