ReasonJun

Next.js : Image component 본문

Frontend/Next.js

Next.js : Image component

ReasonJun 2023. 6. 18. 12:36
728x90

In Next.js, the Image component is a built-in component that provides optimized image loading and rendering. It is specifically designed to handle images and offers several features to improve performance, such as automatic image optimization, lazy loading, and responsive image support. The Image component is part of the next/image package, which is included by default in Next.js projects.

 

Here's an overview of how to use the Image component in Next.js:

  • Import the Image component: To use the Image component, you need to import it from the next/image package:
import Image from 'next/image';
  • Place the Image component in your JSX: You can use the Image component in your JSX by providing the src prop, which specifies the path or URL of the image you want to display. You can also set other optional props to customize the behavior and appearance of the image.
<Image src="/path/to/image.jpg" alt="Description of the image" />
  • Automatic image optimization: Next.js performs automatic image optimization, which means it optimizes the images during the build process. It generates multiple versions of the image with different sizes and formats to provide the best image quality and performance for different devices and network conditions. You don't need to manually resize or optimize the images yourself.

 

  • Responsive image support: The Image component supports responsive images out of the box. It uses the layout prop to define how the image should be displayed. The available layout options are:
  1. layout="intrinsic": The image scales to fit within its container while maintaining its aspect ratio.
  2. layout="responsive": The image takes up the available width and scales proportionally.
  3. layout="fixed": The image has a fixed width and height.

For example, to display a responsive image that scales with the available width, you can use:

<Image src="/path/to/image.jpg" alt="Description of the image" layout="responsive" />
  • Additional props: The Image component provides additional props to customize its behavior, such as width, height, objectFit, objectPosition, and more. You can refer to the Next.js documentation for a comprehensive list of available props and their usage.
  • Automatic image optimization: The Image component automatically optimizes images for performance by using the following techniques:
    • Serving the correct image size for the viewport
    • Using modern image formats, such as WebP and AVIF
    • Lazy loading images
  • Image fit: The Image component allows you to control how images are displayed by using the fit prop. The fit prop can be set to one of the following values:
    • contain: The image will be resized to fit the container, while preserving its aspect ratio.
    • cover: The image will be resized to fill the container, while preserving its aspect ratio. Any excess image will be cropped.
    • fill: The image will be resized to fill the container, and any excess image will be stretched.
import Image from 'next/image';

const MyComponent = () => {
  return (
    
  );
};

It's important to note that the Image component should be used for static images or images that don't require interactivity. For dynamic images or complex use cases, you can use regular HTML img tags.

 

By utilizing the Image component in Next.js, you can benefit from automatic image optimization, responsive image support, and improved performance in your web applications.

728x90
Comments