🖥️ Load and display game images
In this guide, we will learn how to load and display game images from the database. In the previous guide, we added an imageUrl field to our Game model. Now, we’ll update our application to fetch and display these images.
Start the development server
Section titled “Start the development server”If you need to, start the development server by running the following command in your terminal:
npm run devStep 1: Update the GameCard Component Props
Section titled “Step 1: Update the GameCard Component Props”Step 2: Use the Image Prop in the Component
Section titled “Step 2: Use the Image Prop in the Component”Next, we need to update the JSX in our GameCard component to use the image URL passed as a prop instead of the placeholder image.
Can you update the GameCard component to use the imageUrl prop instead of the placeholder image?
- You will need to use the
imageUrlprop as thesrcattribute of the<img>element. - Because this is a variable and not a string, you will need to use curly braces to embed it in the JSX.
- You will also need to update the
altattribute to include the game title. How could you do this?
Even if you get the solution correct, you won’t actually see the images yet.
This is because we need to update the Prisma query to include the image URL, which we will do in the next step.
When you are ready, compare your solution to the one below:
return ( <div className="flex flex-col gap-4"> <div className="relative h-72 overflow-hidden"> <img src="https://picsum.photos/400/300" alt="Game Cover" src={imageUrl} alt={`${title} cover`} className="absolute inset-0 w-full h-full object-cover rounded-xl" /> </div>We’re replacing the hardcoded placeholder image URL with the dynamic imageUrl value that we’re receiving as a prop. We’re also making the alt text more descriptive by including the game title, which improves accessibility.
Step 3: Update the Prisma Query to Include Image Data
Section titled “Step 3: Update the Prisma Query to Include Image Data”Now, we need to update our Prisma query in the loader function to include the image field for each game.
Head back to your app/routes/_index.tsx file and update the Prisma query as follows:
import { PrismaClient } from "@prisma/client";import { json } from "@remix-run/node";import { useLoaderData } from "@remix-run/react";import type { MetaFunction } from "@remix-run/node";import GameCard from "~/components/GameCard";import gamelogFallback from "~/assets/svg/gamelog-fallback.svg"; // You will need to add your own image here
export async function loader() { const prisma = new PrismaClient();
const games = await prisma.game.findMany({ select: { id: true, title: true, releaseDate: true, imageUrl: true, category: { select: { title: true, }, }, }, });
return json({ games });}We’ve added the imageUrl field to our Prisma query’s select object. This ensures that the image URL is included in the data returned from the database.
We’re also importing a fallback image that we’ll use in place of games that don’t have an image.
You will need to add your own fallback image to the assets folder for this to work.
Below is an example of a fallback image I created (feel free to use it if you want!)
Step 4: Pass the Image URL to the GameCard Component
Section titled “Step 4: Pass the Image URL to the GameCard Component”Finally, we need to update our index route to pass the image URL to the GameCard component.
{games.map((game) => ( <div key={game.id}> <GameCard key={game.id} title={game.title} releaseDate={game.releaseDate} imageUrl={game.imageUrl || gamelogFallback} genre={game.category?.title || "Unknown"} /> </div>))}We’re passing the game.imageUrl value as the imageUrl prop to the GameCard component. We’re also using the logical OR operator (||) to provide a fallback image (gamelogFallback) in case a game doesn’t have an image URL. This ensures that our UI always displays an image, even if the data is incomplete.
What We’ve Learned
Section titled “What We’ve Learned”In this tutorial, we’ve:
- Updated our
GameCardcomponent to accept an image URL as a prop - Modified the component to use the provided image URL instead of a placeholder
- Updated our Prisma query to include the image field in the data returned from the database
- Added a fallback image for games that don’t have an image URL
These changes allow our application to display actual game images from our database, making our UI more visually appealing and informative:

Excellent work!
You’ve successfully updated your application to load and display game images from the database. This is a significant improvement to your UI, making it more engaging and informative for users. Remember that handling images is an important aspect of web development, and there are many techniques and best practices to consider as you continue to enhance your application.