Project: "Photo Gallery"
by Andrew Nguyenยท Module 06 - Lists and Keys

Introduction
Not Attempted
NameProgress
Course outline
Not Read
Module 01 - Diving Into React
Not Attempted
NameProgress
Key Concepts
Not Read
Project: Pet Parade
Not Read
Challenge 1
Not Attempted
Quiz 1
Not Attempted
Module 02 - Components and Props
Not Attempted
NameProgress
Project: Profile Card Creator
Not Read
Key Concepts
Not Read
Challenge 2
Not Attempted
Quiz 2
Not Attempted
Module 03 - State and Effect
Not Attempted
NameProgress
Key Concepts
Not Read
Project: Magic Garden
Not Read
Challenge 3
Not Attempted
Quiz 3
Not Attempted
Module 04 - Conditional Rendering
Not Attempted
NameProgress
Key Concepts
Not Read
Project: Weather Wardrobe
Not Read
Challenge 4
Not Attempted
Quiz 4
Not Attempted
Module 05: Handling Events
Not Attempted
NameProgress
Key Concepts
Not Read
Project: "Interactive Quiz App"
Not Read
Challenge 5
Not Attempted
Quiz 5
Not Attempted
Key Concepts
Project: "Photo Gallery"
Challenge 6
Not Attempted
Quiz 6
Not Attempted
Module 07 - Forms and Validation
Not Attempted
NameProgress
Key Concepts
Not Read
Project: "Registration Form"
Not Read
Challenge 7
Not Read
Quiz 7
Not Attempted
Module 08 - Lifting State Up & Composition vs Inheritance
Not Attempted
NameProgress
Key Concepts
Not Read
Project: "Weather Dashboard"
Not Read
Challenge 8
Not Attempted
Quiz 8
Not Attempted
0 / 470 XP

Objective: Build a grid of photos fetched from an API.

Description: In this project, you will create a photo gallery that displays a grid of photos fetched from an external API. This project will help you practice rendering lists, using keys, and fetching data in React.

Instructions:

Step 1: Setup Your Project

  • Create a new React project using Vite.
  • Set up your project structure with a main <strong>App</strong> component and a <strong>PhotoGallery</strong> component.

Step 2: Fetch Photos from an API

Use the <strong>useState</strong> and <strong>useEffect</strong> hooks to fetch photos from an API and store them in a state variable.

Hint: Use the <strong>https://jsonplaceholder.typicode.com/photos</strong> endpoint to fetch sample photos.

import React, { useState, useEffect } from 'react';

function PhotoGallery() {
  const [photos, setPhotos] = useState([]);

  useEffect(() =&gt; {
    fetch('&lt;https://jsonplaceholder.typicode.com/photos&gt;')
      .then(response =&gt; response.json())
      .then(data =&gt; setPhotos(data.slice(0, 20))); // Limit to 20 photos for now
  }, []);

  return (
    &lt;div&gt;
      {/* Render photos here */}
    &lt;/div&gt;
  );
}

export default PhotoGallery;

Step 3: Render Photos in a Grid

Use the <strong>map</strong> method to render the photos in a grid. Each photo should be displayed as an image with a unique <strong>key</strong> prop.

Hint: Use the <strong>url</strong> and <strong>title</strong> properties from the fetched data to display the photos and their titles.

return (
  &lt;div className="photo-gallery"&gt;
    {photos.map(photo =&gt; (
      &lt;div key={photo.id} className="photo-item"&gt;
        &lt;img src={photo.thumbnailUrl} alt={photo.title} /&gt;
        &lt;p&gt;{photo.title}&lt;/p&gt;
      &lt;/div&gt;
    ))}
  &lt;/div&gt;
);

Step 4: Style the Photo Gallery

Add some CSS to style the photo gallery grid. Make the photos display nicely in a grid layout.

Hint: Use CSS grid or flexbox to create a responsive grid layout.

/* Add to your PhotoGallery.css file */
.photo-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
  padding: 20px;
}

.photo-item {
  background-color: #f8f8f8;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  text-align: center;
}

.photo-item img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
}

.photo-item p {
  margin-top: 10px;
  font-size: 14px;
  color: #333;
}

Complete Code Example:

Here's how your complete <strong>PhotoGallery</strong> component might look:

Maggie

Discuss with Maggie
Use the power of generative AI to interact with course content

Discussion

0 comments
Loading editor ...
Remember to be polite and report any undesirable behaviour

Category

Empty

Labels

Discussion has no labels

1 participant

user avatar

Priority

Notifications

You're not receiving notifications from this thread.
Course Outline