react-native-snap-carousel README

The project's README from github.com/meliorence/react-native-snap-carousel. As one markdown file: README.md.

react-native-snap-carousel

platforms npm npm

github issues github closed issues Issue Stats



✨ Some great news for you, fellow plugin user!

💡 Head over there now to learn more about all the goodness that's coming your way.



Table of contents

  1. Showcase
  2. Usage
  3. Example
  4. Props, methods and getters
  5. Layouts and custom interpolations
  6. ParallaxImage component
  7. Pagination component
  8. Tips and tricks
  9. Known issues
  10. Important note regarding Android
  11. Important note regarding iOS
  12. Roadmap
  13. Credits

Showcase

:raised_hands: New feature: layouts

Do you want to find out more?

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

Real-world examples

These are live apps we've created that make heavy use of the plugin. Don't be shy, share yours if you've done something awesome with it!

react-native-snap-carousel aix react-native-snap-carousel aix

react-native-snap-carousel react-native-snap-carousel react-native-snap-carousel


:handshake: Maintainers wanted

Hey there,

Creating and maintaining this plugin has been a fun ride that started in 2016. We thank you all for your appreciation and for making the most out of it! You've motivated us to spend countless hours improving the plugin, and made us happy to give back to the Open Source community.

Put simply, we love this project. However we currently aren't able to give it the love it deserves and the care it requires. If you have enough time and knowledge, and want to become a maintainer, please let us know.

💡 Just head there if you're interested.

We're not abandoning the ship, but we need more people to help us keep it alive and well!


Usage

$ npm install --save react-native-snap-carousel

If you're using Typescript you should also install type definitions:

$ npm install --save @types/react-native-snap-carousel
import Carousel from 'react-native-snap-carousel';

export class MyCarousel extends Component {

    _renderItem = ({item, index}) => {
        return (
            <View style={styles.slide}>
                <Text style={styles.title}>{ item.title }</Text>
            </View>
        );
    }

    render () {
        return (
            <Carousel
              ref={(c) => { this._carousel = c; }}
              data={this.state.entries}
              renderItem={this._renderItem}
              sliderWidth={sliderWidth}
              itemWidth={itemWidth}
            />
        );
    }
}

Example

Here are simple examples that can be edited in real time in your browser:

You can also find a more in-depth (read "complex") one in the /example folder.

react-native-snap-carousel

Props, methods and getters

In order to let you to create mighty carousels and to keep up with your requests, we add new features on a regular basis. Consequently, the list of available props has become really huge and deserves a documentation of its own.

:books: Documentation for "Props, methods and getters"

Layouts and custom interpolations

Built-in layouts

In version 3.6.0, we've added two new layouts on top of the original one: the first one is called 'stack' since it mimics a stack of cards, and the other one is called 'tinder' since it provides a Tinder-like animation.

You can choose between the three of them using the new prop layout and you can modify the default card offset in the 'stack' and 'tinder' layouts with prop layoutCardOffset.

react-native-snap-carousel default layout

<Carousel layout={'default'} />

react-native-snap-carousel stack layout ios react-native-snap-carousel stack layout android

<Carousel layout={'stack'} layoutCardOffset={`18`} />

react-native-snap-carousel tinder layout ios react-native-snap-carousel tinder layout android

<Carousel layout={'tinder'} layoutCardOffset={`9`} />

A few things worth noting:

Custom interpolations

On top of the new layouts, we've exposed the logic we used so that users can create their own awesome layouts! If you're interested, take a deep breath and dive into the dedicated documentation.

:books: Documentation for "Custom interpolations"

Here are a few examples of what can easily be achieved (you can explore the source code and try it live in the provided example):

react-native-snap-carousel custom layout react-native-snap-carousel custom layout react-native-snap-carousel custom layout

ParallaxImage component

Version 3.0.0 introduced a <ParallaxImage /> component, an image component aware of carousel's current scroll position and therefore able to display a nice parallax effect (powered by the native driver to ensure top-notch performance).

:books: Documentation for "ParallaxImage component"

react-native-snap-carousel parallax image

Pagination component

Starting with version 2.4.0, a customizable <Pagination /> component has been added. You can see below how it looks like with its default configuration.

:books: Documentation for "Pagination component"

react-native-snap-carousel pagination

Tips and tricks

We've gathered together all the useful tips and tricks. There is a bunch of them, which makes this section a must-read!

:books: Documentation for "Tips and tricks"

Known issues

Make sure to read about the known issues before opening a new one; you may find something useful.

:books: Documentation for "Known issues"

Important note regarding Android

react-native-snap-carousel android

Android's debug mode is a mess: timeouts regularly desynchronize and scroll events are fired with some lag, which completely alters the inner logic of the carousel. On Android, you will experience issues with carousel's behavior when JS Dev Mode is enabled, and you might have trouble with unreliable callbacks and loop mode when it isn't. This is unfortunate, but it's rooted in various flaws of ScrollView/FlatList's implementation and the miscellaneous workarounds we had to implement to compensate for it.

:warning: Therefore you should always check if the issue you experience also happens in a production environment. This is, sadly, the only way to test the real performance and behavior of the carousel.

For more information, you can read the following notes: "Android performance" and "Unreliable callbacks".

Important note regarding iOS

react-native-snap-carousel ios

:warning: When debugging with the iOS simulator, you're only one "Cmd + T" away from toggling "Slow Animations". If carousel's animations seem painfully slow, make sure that you haven't enabled this setting by mistake.

Roadmap

Credits

Written by Benoît Delmaire (bd-arc) and Maxime Bertonnier (Exilz) at Meliorence.

react-native-snap-carousel (meliorence/react-native-snap-carousel)

Searching Database…

这是什么?这是一个 React Native 的轮播/滑动切换组件——一个添加到移动应用里的库,用来渲染可以滑动的卡片轮播,支持条目预览、多种布局、视差图片,而且能高性能地处理大量条目。
怎么把它加到我的项目里?用 npm 把 react-native-snap-carousel 包安装到你的 React Native 项目里;如果用 TypeScript,还需要加上类型定义。README 里提供了简单的示例,你写任何代码之前都可以先在浏览器里直接修改、实时预览效果。
我的应用需要满足什么条件才能用它?你需要一个已有的 React Native 应用,因为这个组件是通过原生的 Android/iOS 视图来渲染的。运行时依赖很少:只需要 prop-types 和 react-addons-shallow-compare。
那些花哨的布局有什么坑吗?有——'stack' 和 'tinder' 这两种布局为了避免渲染问题,改用 ScrollView 而不是 FlatList 来渲染,因此会失去 FlatList 的性能优化。README 里明确警告不要在数据量很大时使用它们。
有哪些布局可以选择?内置三种布局:默认布局、'stack'(模仿一摞卡片叠放的效果)和 'tinder'(类似 Tinder 的动画效果),通过 layout 属性选择,还可以用 layoutCardOffset 微调。作者也公开了他们的实现逻辑,所以你可以编写自定义插值,设计自己的布局。这些新布局是为横向轮播设计的,但纵向也能用。
调试的时候它的表现会一样吗?不一定。在 Android 上,JS Dev Mode 会让定时器失去同步、滚动事件出现延迟,从而破坏轮播组件的内部逻辑,回调函数和循环模式也可能不可靠——所以排查问题时要始终用生产版本构建来验证。在 iOS 模拟器上,如果动画慢得难以忍受,请确认你没有开启 'Slow Animations'(Cmd + T)。
除了轮播本身,还附带哪些东西?附带一个 ParallaxImage 组件,它会根据轮播的滚动位置做出反应,实现由原生驱动支持的视差效果;另外还有一个可自定义的 Pagination 分页组件(从 v2.4.0 起提供)。路线图显示,循环模式、RTL(从右到左布局)、纵向轮播和单条数据支持也都已经完成。
这个项目还在积极维护吗?它已经相当成熟、使用广泛(始于 2016 年,GitHub 上 star 超过 1 万),但原作者表示他们已经无力投入所需的精力,正在公开寻找新的维护者——不过他们也强调并不是要放弃这个项目。
Creator
meliorence
Size
1.3 MB
Stars
10.5K
Age
10y
Commits · 90 Days