Lesson 2 - BookShelf App Structure
Last updated
Last updated
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 the SearchBar.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.
AppBookShelf.jsx
parent componentSetup:
Import the required libraries and components.
Initialize the state variables using useState
.
Fetch API Data:
Define fetchBooks
to fetch and parse data from the Google Books API.
Handle errors gracefully.
React Lifecycle:
Use useEffect
to ensure the API is called when the component loads or when query
changes.
Handle User Input:
Define a function (handleSearch
) to update the query
based 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.
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.
useState
and useEffect
: 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 books
state.
Handles errors with try...catch
.
4. Using useEffect
for Side Effects
Purpose:
Automatically fetches data when the component mounts or when the query
state 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 query
state, which triggers useEffect
to 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 query
and handleSearch
as props.
Displays the header and search bar.
BookList
:
Receives books
and query
as props.
Dynamically displays the list of books fetched from the API.
Footer
:
Displays footer content like copyright and social media links.
Classes:
container
: Ensures a responsive layout.
mt-5
: Adds top margin for spacing.
Provides a clean and professional look without writing custom CSS.
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.