📖 Scenario: You are building a simple Next.js website that shows a product image. You want to use Next.js's built-in image optimization to make the page load faster and look good on all devices.
🎯 Goal: Create a Next.js component that displays an optimized image using the next/image component with proper width, height, and alt text.
📋 What You'll Learn
Import the Image component from next/image
Use the Image component to display an image from the public folder
Set the image src to /product.jpg
Set the image width to 400 and height to 300
Add an alt attribute with the text Product image
💡 Why This Matters
🌍 Real World
Optimizing images on websites improves loading speed and user experience, especially on mobile devices and slow networks.
💼 Career
Knowing how to use Next.js's image optimization is valuable for frontend developers working on performance-focused React applications.
Progress0 / 4 steps
1
Setup the Next.js component
Create a new React functional component called ProductImage that returns an empty fragment <></>.
NextJS
Hint
Start by writing a simple React function that returns an empty fragment.
2
Import the Image component
Add an import statement at the top to import Image from next/image.
NextJS
Hint
Use import Image from 'next/image' to get the optimized image component.
3
Add the Image component with src, width, height, and alt
Inside the ProductImage component's return, replace the empty fragment with the Image component. Set src to "/product.jpg", width to 400, height to 300, and alt to "Product image".
NextJS
Hint
Use the Image component with the exact props for source, size, and alt text.
4
Export the component as default
Add a default export statement for the ProductImage component at the end of the file.
NextJS
Hint
Use export default ProductImage; to make the component usable in other files.
Practice
(1/5)
1. What is the main benefit of using next/image in a Next.js project?
easy
A. It only works with SVG images.
B. It converts images to text for SEO purposes.
C. It disables image loading to save bandwidth.
D. It automatically optimizes images for faster loading and better quality.
Solution
Step 1: Understand the purpose of next/image
The next/image component is designed to optimize images automatically, improving loading speed and quality.
Step 2: Compare options with the purpose
Only It automatically optimizes images for faster loading and better quality. correctly describes this benefit. Other options describe unrelated or incorrect features.
Final Answer:
It automatically optimizes images for faster loading and better quality. -> Option D
A. The image loads immediately with 200x100 size due to priority prop.
B. The image will not render because priority is invalid.
C. The image loads lazily by default with 200x100 size.
D. The image loads with default size ignoring width and height.
Solution
Step 1: Understand the priority prop effect
The priority prop forces the image to load immediately instead of lazy loading.
Step 2: Check width and height usage
Width and height props set the image size to 200x100 pixels, which will be respected.
Final Answer:
The image loads immediately with 200x100 size due to priority prop. -> Option A
Quick Check:
priority = immediate load = C [OK]
Hint: priority prop disables lazy loading for faster display [OK]
Common Mistakes:
Assuming priority disables image rendering
Thinking width and height are ignored
Confusing lazy loading default behavior
4. Identify the error in this Next.js image usage and how to fix it:
import Image from 'next/image';
export default function Profile() {
return (
<Image src='/profile.jpg' alt='User Profile' width='150' height='150' />
);
}
medium
A. src path must be an external URL, not local path.
B. alt attribute is missing, causing accessibility issues.
C. width and height should be numbers, not strings.
D. Image component must be self-closing with a slash.
Solution
Step 1: Check width and height prop types
Next.js Image requires width and height as numbers, not strings. Here they are strings ('150').
Step 2: Correct the prop types
Remove quotes to pass numbers: width={150} and height={150}.
Final Answer:
width and height should be numbers, not strings. -> Option C
Quick Check:
width/height as numbers = A [OK]
Hint: Use curly braces for numeric width and height [OK]
Common Mistakes:
Passing width and height as strings
Ignoring alt attribute importance
Thinking local src paths are invalid
5. You want to optimize multiple images with different sizes responsively using next/image. Which approach correctly uses the sizes attribute to serve appropriate image sizes on different screen widths?
D. <Image src='/photo.jpg' alt='Photo' width={800} height={600} />
Solution
Step 1: Understand the sizes attribute usage
The sizes attribute defines image display sizes for different viewport widths using media queries.
Step 2: Analyze the options
<Image src='/photo.jpg' alt='Photo' width={800} height={600} sizes='(max-width: 600px) 100vw, 50vw' /> correctly uses (max-width: 600px) 100vw, 50vw meaning full viewport width on small screens, half on larger screens. This is a common responsive pattern.
Final Answer:
Use sizes='(max-width: 600px) 100vw, 50vw' with numeric width and height. -> Option A
Quick Check:
Responsive sizes attribute = A [OK]
Hint: Use sizes with max-width media query for responsive images [OK]
Common Mistakes:
Passing width and height as strings
Using min-width instead of max-width incorrectly
Omitting sizes attribute for responsiveness
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.