Gatsby Image components - StaticImage vs GatsbyImage

2022. 12. 7.·🎨 프론트엔드 공부/기타

📍Gatsby Image components 2가지

1️⃣StaticImage

- Static image

- 컴포넌트나 템플릿에 관계 없이 항상 똑같은 이미지이어야 할 때

- 예시

import { StaticImage } from "gatsby-plugin-image"

export function Dino() {
  return (
    <StaticImage
      // src 어트리뷰트에는 변수 전달 불가
      // 오직 url 또는 로컬 경로만 지정 가능
      src="../images/dino.png"
      alt="A dinosaur"
      placeholder="blurred"
      layout="fixed"
      width={200}
      height={200}
    />
  )
}

 

2️⃣GatsbyImage

- Dynamic image

- CMS로부터의 데이터 또는 컴포넌트에서 전달받은 값에 따라 이미지가 달라져야 할 때

 

- 이미지를 포함하는 모든 GraphQL 객체는 childImageSharp 필드 형태로 존재

- gatsbyImageData 리졸버를 통해 이미지를 설정

 

- getImage 함수를 통해 이미지를 쉽게 꺼낼 수 있다

( .childImageSharp.gatsbyImageData 를 생략할 수 있음 )

 

- 예시

import { graphql } from "gatsby"
import { GatsbyImage, getImage } from "gatsby-plugin-image"

function BlogPost({ data }) {
  const image = getImage(data.blogPost.avatar)
  return (
    <section>
      <h2>{data.blogPost.title}</h2>
      <GatsbyImage image={image} alt={data.blogPost.author} />
      <p>{data.blogPost.body}</p>
    </section>
  )
}

export const pageQuery = graphql`
  query ($id: String) {
    blogPost(id: { eq: $id }) {
      title
      body
      author
      avatar {
        childImageSharp {
          gatsbyImageData(
            width: 200
            placeholder: BLURRED
            formats: [AUTO, WEBP, AVIF]
          )
        }
      }
    }
  }
`

 

'🎨 프론트엔드 공부/기타' 카테고리의 다른 글
  • [Gatsby & GraphQL] gatsbyImageData 리졸버
  • [Gatsby & Contentful] sourcing rich text from Contenful
  • ESLint & Prettier for React & TypeScript
  • Vite - 매우 빠른 React 개발 환경
지식물원
지식물원
지식이 자라는 식물원!
  • 지식물원
    지식물원
    지식물원
  • 전체
    오늘
    어제
    • 분류 전체보기 (516)
      • 🎨 프론트엔드 공부 (253)
        • JS & TS (92)
        • HTML & CSS (22)
        • React & Next (49)
        • Vue & Nuxt (22)
        • 기타 (68)
      • 🤓 기술 학습 & 공부 기록 (116)
        • Node.js (0)
        • Python (37)
        • 백엔드 (0)
        • 딥러닝 (1)
        • 컴퓨터 일반 (72)
        • 개발 인프라 (6)
      • 👨‍💻 프로젝트 경험 (6)
        • Work (0)
        • Toy (6)
      • ⚙️ 개발 팁 & 노하우 (21)
        • 프론트엔드 (6)
        • 기타 (15)
      • ☕️ 커리어 & 인터뷰 준비 (88)
        • 코딩 테스트 (88)
      • 📰 기술 트렌드 & 생각 정리 (4)
      • 📚 기타 (25)
        • 마케팅 (15)
        • 비개발서적 (10)
  • 블로그 메뉴

    • 태그
  • 링크

  • 공지사항

    • 모바일 접속 시 코드 하이라이팅 깨질 때
  • 인기 글

  • hELLO· Designed By정상우.v4.10.3
지식물원
Gatsby Image components - StaticImage vs GatsbyImage
상단으로

티스토리툴바