The goal of this React application is to fetch and display product information from the Dummy JSON Products API. The app uses modern React features, such as hooks (useState and useEffect), and makes asynchronous API requests using Axios.
-
Clone the repository:
git clone https://github.com/your-username/your-repo.git
-
Install dependencies:
npm install
-
Run the application:
npm start
-
Open your browser and navigate to
http://localhost:3000to view the app.
To fetch data from the Dummy JSON Products API, the application uses the axios library and React hooks. Here's a breakdown of the relevant code:
- The
useEffecthook is used to handle side effects in functional components. In this case, it's fetching data from the API. - Inside the
useEffect, an asynchronous functionfetchDatais defined to make the API call usingaxios.get. - Upon a successful response, the
setDatafunction is used to update the state with the fetched product data. - Error handling is implemented using a
try...catchblock to log any errors that might occur during the API request. - The
fetchDatafunction is invoked within theuseEffect, ensuring that the data is fetched when the component mounts.
import React, { useState, useEffect } from "react";
import axios from "axios";
import "./App.css";
export default function App() {
// State to hold the fetched data
const [data, setData] = useState([]);
// useEffect hook to perform the data fetching
useEffect(() => {
// Async function to fetch data
async function fetchData() {
try {
// Make a GET request to the API endpoint
const response = await axios.get("https://dummyjson.com/products");
// Update the state with the fetched data
setData(response.data.products);
} catch (error) {
// Handle errors if any
console.error("Error fetching data:", error);
}
}
// Invoke the fetchData function
fetchData();
}, []); // Empty dependency array ensures the effect runs only once, equivalent to componentDidMount
// Code to render the fetched data in a card layout (not shown in this snippet)
let show = data.slice(0, 15).map((res, index) => (
// ... (your card component)
));
// Return the JSX rendering
return <div className="parent">{show}</div>;
}Feel free to customize this template based on your project's specifics. Add any additional sections or details that you think would be helpful for users exploring or contributing to your project.