const sliderVisibleWidth = slider.offsetWidth;
function getTotalItemsWidth(items) {
const { left } = items[0].getBoundingClientRect();
const { right } = items[items.length - 1].getBoundingClientRect();
return right - left;
}
const totalItemsWidth = getTotalItemsWidth(items);
const maxXOffset = 0;
const minXOffset = - (totalItemsWidth - sliderVisibleWidth);
문제 키때문에 뭔가 가다가 멈춘다
Why do keys change when children are used with React.Children.toArray?
swiper/src/react at master · nolimits4web/swiper
import React from 'react';
function processChildren(c) {
const slides = [];
React.Children.toArray(c).forEach((child) => {
if (child.type && child.type.displayName === 'SwiperSlide') {
slides.push(child);
} else if (child.props && child.props.children) {
processChildren(child.props.children).forEach((slide) => slides.push(slide));
}
});
return slides;
}
function getChildren(c) {
const slides = [];
const slots = {
'container-start': [],
'container-end': [],
'wrapper-start': [],
'wrapper-end': [],
};
React.Children.toArray(c).forEach((child) => {
if (child.type && child.type.displayName === 'SwiperSlide') {
slides.push(child);
} else if (child.props && child.props.slot && slots[child.props.slot]) {
slots[child.props.slot].push(child);
} else if (child.props && child.props.children) {
const foundSlides = processChildren(child.props.children);
if (foundSlides.length > 0) {
foundSlides.forEach((slide) => slides.push(slide));
} else {
slots['container-end'].push(child);
}
} else {
slots['container-end'].push(child);
}
});
return { slides, slots };
}
export { getChildren };
React로 Carousel(캐러셀) 만들기 (3) - translateX를 이용한 구현