Lesson 2 - BookShelf App Structure
Google BookShelf React App v1

App Structure
We are going to build Google Bookshelf React App with the following components:
Parent component : AppBookShelf.jsx contains three child components – NavBar.jsx, BookList.jsx and Footer.jsx. The children components are put in components folder.
Child components in components folder :
NavBar.jsx: This component includes the application header title and theSearchBar.jsx, both located in the same directory.BookList.jsx: This component is responsible for displaying a list of book information fetched from an API endpoint. It accepts props for the title, authors, description, and publisher, as well as additional query props.Footer.jsx: Contains copyright information along with social media links for Instagram, YouTube, Facebook, and X.
The code for the AppBookShelf.jsx parent component
AppBookShelf.jsx parent componentStep-by-Step Explanation for Students
Setup:
Import the required libraries and components.
Initialize the state variables using
useState.
Fetch API Data:
Define
fetchBooksto fetch and parse data from the Google Books API.Handle errors gracefully.
React Lifecycle:
Use
useEffectto ensure the API is called when the component loads or whenquerychanges.
Handle User Input:
Define a function (
handleSearch) to update thequerybased on user input.
Render the UI:
Divide the app into child components (
NavBar,BookList,Footer) for better modularity.Pass props to child components to maintain communication between them.
Layout Structure of AppBookShelf Component
Explanation of Code Structure
App Structure
API End Point: https://www.googleapis.com/books/v1/volumes?q=${query}
Import Libraries and Components:
React: Core library for building UI.
useState and useEffect: Hooks for managing state and side effects.
Child Components:
NavBar,BookList, andFooterfor modularity.
State Management:
books: Holds the book data fetched from the API.query: Stores the user's search term.
Fetching Data:
fetchBooksis an asynchronous function that retrieves book data from the Google Books API.Updates the
booksstate with the fetched results or an empty array if no data is returned.
Effect Hook:
Ensures
fetchBooksruns when the component mounts or thequerystate changes.
Handle Search:
handleSearchis a function that updates thequerybased on user input received from theNavBarcomponent.
Child Components:
NavBar: Contains the search bar and passes the query handling logic.BookList: Receivesbooksandqueryas props and displays book data.Footer: Displays copyright and social media links.
Bootstrap Integration:
Styling is done using Bootstrap classes for a professional look.
Explanation of AppBookShelf.jsx Code Structure and Snippets
AppBookShelf.jsx Code Structure and SnippetsThe code is structured step-by-step to make it easy for students to understand how the parent component manages data and communicates with its child components. Here's a detailed breakdown:
1. Importing Libraries and Components
React: Core library for creating React components.useStateanduseEffect: Hooks for managing state and side effects.Child Components:
NavBar: Contains the search bar and header.BookList: Displays the list of books.Footer: Contains footer content (e.g., copyright and social links).
Bootstrap: A CSS framework for styling the app.
2. State Management
books:Stores the array of books fetched from the API.
Initially set to an empty array.
query:Stores the search term entered by the user.
Defaults to
"react"to show results when the app first loads.
3. Fetching Data from the API End Point
API Endpoint:
Fetches book data using the search term stored in
query.
Steps:
Makes an HTTP GET request using
fetch.Checks if the response is successful (
response.ok).Parses the response JSON and updates the
booksstate.Handles errors with
try...catch.
4. Using useEffect for Side Effects
Purpose:
Automatically fetches data when the component mounts or when the
querystate changes.
Dependency Array:
[query]ensures the function re-runs only when the search term changes.
5. Handling Search Input
Functionality:
Called back when the user enters a new search term.
Updates the
querystate, which triggersuseEffectto fetch new data.
6. Rendering the Component - NavBar, BookList, Footer components
Container: The entire app is wrapped in a Bootstrap container for layout.
Child Components:
NavBar:Receives
queryandhandleSearchas props.Displays the header and search bar.
BookList:Receives
booksandqueryas props.Dynamically displays the list of books fetched from the API.
Footer:Displays footer content like copyright and social media links.
Bootstrap Integration
Classes:
container: Ensures a responsive layout.mt-5: Adds top margin for spacing.
Provides a clean and professional look without writing custom CSS.
Outcome
By following these steps:
Students can understand how to manage data and user interactions in a React app.
They learn how to fetch, manage, and display data from an external API.
They gain insight into component-based architecture and modular design.
Last updated