Things can become hard to manage when you need to display more than one item, but not for React Spring Carousel 🦾
import { useSpringCarousel } from 'react-spring-carousel'
export function Component() {
const {
carouselFragment,
slideToPrevItem,
slideToNextItem
} = useSpringCarousel({
itemsPerSlide: 3
withLoop: true,
items: mockedItems.map((i) => ({
id: i.id,
renderItem: (
<CarouselItem color={i.color}>
{i.title}
</CarouselItem>
),
})),
});
return (
<div>
<button onClick={slideToPrevItem}>Prev item</button>
{carouselFragment}
<button onClick={slideToNextItem}>Next item</button>
</div>
);
}
If you want to, you can also change the starting position of the initial active item; the accepted values are:
import { useSpringCarousel } from 'react-spring-carousel'
export function Component() {
const {
carouselFragment,
slideToPrevItem,
slideToNextItem
} = useSpringCarousel({
initialStartingPosition: 'start' | 'center' | 'end',
items: mockedItems.map((i) => ({
id: i.id,
renderItem: (
<CarouselItem color={i.color}>
{i.title}
</CarouselItem>
),
})),
});
return (
<div>
{carouselFragment}
</div>
);
}
You can also easily add some space between the items:
import { useSpringCarousel } from 'react-spring-carousel'
export function Component() {
const {
carouselFragment,
slideToPrevItem,
slideToNextItem
} = useSpringCarousel({
gutter: 24,
itemsPerSlide: 3,
withLoop: true,
items: mockedItems.map((i) => ({
id: i.id,
renderItem: (
<CarouselItem color={i.color}>
{i.title}
</CarouselItem>
),
})),
});
return (
<div>
{carouselFragment}
</div>
);
}
You can also add gutter on the sides of the visible items:
import { useSpringCarousel } from 'react-spring-carousel'
export function Component() {
const {
carouselFragment,
slideToPrevItem,
slideToNextItem
} = useSpringCarousel({
itemsPerSlide: 3,
withLoop: true,
startEndGutter: 24,
items: mockedItems.map((i) => ({
id: i.id,
renderItem: (
<CarouselItem color={i.color}>
{i.title}
</CarouselItem>
),
})),
});
return (
<div>
{carouselFragment}
</div>
);
}