Open SirKadogan opened 4 years ago
this is great, thanks @SirKadogan!
here's the exact same code in pure javascript for anyone who comes across this and needs an easy/copy and paste:
import React, { useState, useCallback, useRef } from 'react';
import { Text, View, SafeAreaView } from 'react-native';
import Carousel from 'react-native-snap-carousel';
const exampleItems = [
{
title: 'Item 1',
text: 'Text 1',
},
{
title: 'Item 2',
text: 'Text 2',
},
{
title: 'Item 3',
text: 'Text 3',
},
{
title: 'Item 4',
text: 'Text 4',
},
{
title: 'Item 5',
text: 'Text 5',
},
];
const CustomCarousel = () => {
const [activeIndex, setActiveIndex] = useState(0);
const [carouselItems, setCarouselItems] = useState(exampleItems);
const ref = useRef(null);
const renderItem = useCallback(({ item, index }) => (
<View
style={{
backgroundColor: 'floralwhite',
borderRadius: 5,
height: 250,
padding: 50,
marginLeft: 25,
marginRight: 25,
}}
>
<Text style={{ fontSize: 30 }}>{item.title}</Text>
<Text>{item.text}</Text>
</View>
), []);
return (
<SafeAreaView style={{ flex: 1, backgroundColor: 'rebeccapurple', paddingTop: 50 }}>
<View style={{ flex: 1, flexDirection: 'row', justifyContent: 'center' }}>
<Carousel
layout="default"
ref={ref}
data={carouselItems}
sliderWidth={300}
itemWidth={300}
renderItem={renderItem}
onSnapToItem={(index) => setActiveIndex(index)}
/>
</View>
</SafeAreaView>
);
};
export default CustomCarousel;
I created pull request #800 to add this javascript example to the Readme
Sorry, please allow me to advertise for my open source library! ~ I think this library react-native-reanimated-carousel will solve your problem. It is a high performance and very simple component, complete with React-Native reanimated 2
I created pull request #800 to add this javascript example to the Readme
Awesome 👌
Is this a bug report, a feature request, or a question?
Feature request
Have you followed the required steps before opening a bug report?
Yes
Description
I would suggest the the addition of a basic example using a functional component, as it is mostly the standard in React at the moment. Since the library also includes typescript, an example for that would be nice as well.
If you would introduce me how to submit these changes to the repo, I'd gladly do that.
I've submitted a typescript example here already: #642
And here's my suggestion for a basic functional component (also using typescript, but easy to update for JS):