Notice
Recent Posts
Recent Comments
Link
250x250
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Git clone
- maxwidth
- node 오류
- 플러그인
- react npm install
- window 정책변경
- 글자들여쓰기
- npm install
- legacy-peer
- googleicon
- MediaQuery
- XEIcon
- package.json
- node설치
- vscode git clone
- 이미지반응형
- slickslider
- 그누보드반응형
- fontawesome
- 동적객체
- npm install 문제
- 정적객체
- minwidth
- 의존성문제
- 웹아이콘
- owlcarousel
- npm start
- 단어단위로떨어지기
- git lab clone
- 아이콘사용법
Archives
- Today
- Total
어쩌다 알게 된 ƪ(•̃͡•̃͡ ƪ
useRef로 resize width 계산 후 class 넣기 본문
728x90
useRef 사용해서 DOM에 있는 태그를 잡아옴
그리고 .current를 붙여서 렌더링 된 태그를 잡음
const [isWrapped, setIsWrapped] = useState(false);
const userRestInfoBoxRef = useRef(null);
const firstItemRef = useRef(null);
const secondItemRef = useRef(null);
const [startIndex, setStartIndex] = useState(0);
const widthCheck = useCallback(() => {
const userRestInfoBoxWidth = userRestInfoBoxRef.current?.offsetWidth - 18.5 || 0;
const firstItemWidth = firstItemRef.current?.offsetWidth || 0;
const secondItemWIdth = secondItemRef.current?.offsetWidth || 0;
const sumItemWidth = firstItemWidth + secondItemWIdth;
setIsWrapped(sumItemWidth > userRestInfoBoxWidth); // 부모 크기보다 크면 true
console.log('hyobin_______test', userRestInfoBoxWidth, firstItemWidth, secondItemWIdth);
}, []);
useEffect(() => {
widthCheck();
window.addEventListener('resize', widthCheck);
return () => window.removeEventListener('resize', widthCheck);
}, [widthCheck]);
useEffect(() => {
widthCheck();
}, [startIndex]);
<div
className={`vacation_status_box ${mobileDesign && isWrapped ? 'type_wrap' : ''}`}
ref={userRestInfoBoxRef}
>
<div className='d-flex flex-row justify-content-between align-items-center'>
<div ref="firstItemRef">계속 바뀌는 너비</div>
<div ref="secondItemRef">계속 바뀌는 너비2</div>
<div className='btn_arrow_box'>
화살표
</div>
</div>
</div>
728x90
'개발 > 🟦 React' 카테고리의 다른 글
| ReactSortable 드래그, 솔팅 기능_리액트 라이브러리 (0) | 2025.12.31 |
|---|---|
| 다국어 처리 useTranslation() 훅 사용 (0) | 2025.12.03 |
| useFormik 값이 무얼 뜻하는지 backend에서 찾기 (서버 전송) (0) | 2025.11.28 |
| <Portal>이란? _ 보통 모달을 많이 씀 (0) | 2025.11.26 |
| 컴포넌트의 옵션 props 기본 지정_컴포넌트에 지정하면 안되나? (1) | 2025.11.25 |
Comments