14

enter image description here

Does anyone know how can we achieve this kind of view in React Native, or is there any available components out there that can help on this issue?

I've seen in F8 2016 app too, been searching on how to achieve the transition and the carousel-like view with horizontal scrolling.

vizFlux
  • 735
  • 2
  • 9
  • 14

4 Answers4

21

I know that the question is old, but a co-worker and I recently had to create a component that answers this particular need. We ended up open-sourcing it, so it's all yours to try: react-native-snap-carousel.

The plugin is now built on top of FlatList (versions >= 3.0.0), which is great to handle huge numbers of items. It also provides previews (the effect you were after), snapping effect, parallax images, RTL support, and more.

You can take a look at the showcase to get a grasp of what can be achieved with it. Do not hesitate to share your experience with the plugin since we're always trying to improve it.

react-native-snap-carousel archriss showcase react-native-snap-carousel archriss aix


Edit : two new layouts have been introduced in version 3.6.0 (one with a stack of cards effect and the other with a tinder-like effect). Enjoy!

react-native-snap-carousel stack layout react-native-snap-carousel tinder layout

bend
  • 1,384
  • 14
  • 22
  • 1
    I'd be wary of this plugin as well. There haven't been any updates to it since Feb 2018 – Mike S. Jun 07 '18 at 15:38
  • 1
    Hey Mike, let me assure you that the plugin is still alive and well. The latest merged update was made two months ago, but a lot of work is happening behind the scene. And, if you take a close look at the issues, you'll see that every one of them has been answered within hours. Still, good contributors are always welcome if you want to help ;-) – bend Jun 07 '18 at 16:56
  • 3
    Thanks, @bend. I'll keep an eye on it. Still, before I add a library to an enterprise app, I need assurance that it stays current with the RN library. – Mike S. Jun 07 '18 at 23:11
  • So far the library has many open issues and pulled requests are stacking up. I wonder what the code owners are doing to push things forward. – Dr. Younes Henni Dec 03 '19 at 11:46
  • This is pretty unstable and flaky, especially number of cards are more. – mixdev Feb 22 '22 at 20:02
16

You can achieve this using ScrollView with paging enabled on iOS and ViewPagerAndroid on Android.

F8 being an open source app, you can see that's what it's actually using: https://github.com/fbsamples/f8app/blob/master/js/common/ViewPager.js

This component renders all pages.

If you only want to have the visible and left and right pages rendered to save memory, there's another component built on top of it that does it: https://github.com/fbsamples/f8app/blob/master/js/common/Carousel.js

There are various other similar implementations available:

However I'm not recommending https://github.com/leecade/react-native-swiper as I've had several issues with it.

Charlino
  • 15,802
  • 3
  • 58
  • 74
Jean Regisser
  • 6,636
  • 4
  • 32
  • 32
  • 2
    @jean-regisser what are the major issues you have ran into with `react-native-swiper` ? – Marty Jan 08 '17 at 11:09
  • 1
    `react-native-swiper` currently has a major android bug. It doesn't display at all on android. There are multiple issues and no recently accepted PR's. I think the module has been abandoned. – Mike S. Jun 07 '18 at 15:35
  • @MikeS. As of November 2018, it looks there are some commits for `reat-native-swiper`. The only Android-issue I got is a problem with the fonts, see my answer below. – B--rian Nov 23 '18 at 10:42
0

Speaking about the swiper-component claiming the best of the world, it still does not work out of the box (as of November 2018) as described in the official swiper-react-native documentation. The issue and a workaround is described in the swiper issue 444:

The error message (on Android) states console.error: "fontFamily 'Arial' is not a system font and has not been loaded through Exponent.Font.loadAsync.

Zach Dixon provided an elegant quick-fix which I repeat here for everybody's convenience. Simply use the following JSX-snippet inside your render()-function to avoid that a new font is required:

<Swiper style={styles.wrapper} showsButtons={true} 
        nextButton={<Text>&gt;</Text>} prevButton={<Text>&lt;</Text>}>
  <View style={styles.slide1}><Text style>Slide 1</Text></View>
  <View style={styles.slide2}><Text style>Slide 2</Text></View>
  <View style={styles.slide3}><Text style>Slide 3</Text></View>
</Swiper>

For those interested in explanations on how to implement carousel with Scroll-View only, I recommend a tutorial on a simple image carousel with ScrollView. The tutorial is straight forward and elaborates on the things one has to take care of, but you cannot use it out of the box within or on top of other View-elements. In particular the snapping does not work to well (on Android).

B--rian
  • 5,578
  • 10
  • 38
  • 89
0

You can create your own custom carousel. The Carousel end result looks like this-

enter image description here

     goToNextPage = () => {
    const childlenth = this.getCustomData().length;
    selectedIndex = selectedIndex + 1;
    this.clearTimer();
    if (selectedIndex === childlenth) {
        this.scrollRef.current.scrollTo({ offset: 0, animated: false, nofix: true });
        selectedIndex = 1;
    }
    this.scrollRef.current.scrollTo({
        animated: true,
        x: this.props.childWidth * selectedIndex,
    });
    this.setUpTimer();
}

// pushing 1st element at last
 getCustomData() {
    const {data} = this.props;
    const finaldata = [];
    finaldata.push(...data);
    finaldata.push(data[0]);
    return finaldata;
}

This is the main logic used behind looped carousel. Here we are pushing the first item at last in the list again and then when scroll reaches at last position we are making the scrollview to scroll to first position as first and last element are same now and we scroll to first position with animation like this

this.scrollRef.current.scrollTo({ offset: 0, animated: false, nofix: true });

For further reference go through the link provided.

https://goel-mohit56.medium.com/custom-horizontal-auto-scroll-looped-carousel-using-scrollview-42baa5262f95

Mohit Goel
  • 722
  • 1
  • 8
  • 18