useResponsiveValue
뷰포트 크기에 값을 매핑한 객체에서 현재 뷰포트에 맞는 값을 가져오는 훅이에요. narrow/regular/wide 같은 키에 값을 지정하고, 두 번째 인자로 기본값을 주면 브라우저 크기에 따라 알맞은 값이 반환돼요.
출처: 문서
본문
뷰포트 크기에 값을 매핑한 객체에서 값을 가져와요. 이 훅은 뷰포트 크기에 매핑된 값들을 가진 객체에 사용돼요.
예제
브라우저 창 크기를 바꾸면 버튼 라벨이 바뀌는 걸 볼 수 있어요.
import React from 'react'
// eslint-disable-next-line primer-react/no-use-responsive-value
import {Button, IconButton, useResponsiveValue} from '@primer/react'
import {DownloadIcon} from '@primer/octicons-react'
export default function Default() {
const label = useResponsiveValue(
{
narrow: null,
regular: 'Download',
wide: 'Download all assets',
},
'Download',
)
if (!label) {
return <IconButton icon={DownloadIcon} aria-label="Download all assets" />
}
return <Button leadingVisual={DownloadIcon}>{label}</Button>
}
API
useResponsiveValue의 전체 API는 원문 페이지의 "API" 섹션에서 확인할 수 있어요.
더 알아보기 (Learn more)
- 첫 인자로
{narrow, regular, wide}처럼 뷰포트 크기별 값 객체를, 두 번째 인자로 기본값을 넘겨요. - 값이 비어 있으면(예: narrow에서
null) 다른 UI로 대체할 수 있어요. - 반응형 라벨·레이아웃을 화면 폭에 맞게 바꿀 때 유용해요.