Best React.js Swiper Components
In this article we want to explore the best components you can use as swiper in your web app. These are third party components that you install to give the swipe functionality which makes your app more modern.
(a). react-swipeable-views
A React component for swipeable views.
Installation
Browser
npm install --save react-swipeable-views
Native (experimental)
npm install --save react-swipeable-views-native
Simple example
Browser
import React from 'react';
import SwipeableViews from 'react-swipeable-views';
const styles = {
slide: {
padding: 15,
minHeight: 100,
color: '#fff',
},
slide1: {
background: '#FEA900',
},
slide2: {
background: '#B3DC4A',
},
slide3: {
background: '#6AC0FF',
},
};
const MyComponent = () => (
<SwipeableViews>
<div style={Object.assign({}, styles.slide, styles.slide1)}>
slide n°1
</div>
<div style={Object.assign({}, styles.slide, styles.slide2)}>
slide n°2
</div>
<div style={Object.assign({}, styles.slide, styles.slide3)}>
slide n°3
</div>
</SwipeableViews>
);
export default MyComponent;
Native (experimental)
react-native support is experimental and I have no plan pushing it forward. I start to think that lower level abstraction to share the implementation between the platforms are more appropriate. We have two different implementations of the react-swipeable-views API.
import React from 'react';
import {
StyleSheet,
Text,
View,
} from 'react-native';
import SwipeableViews from 'react-swipeable-views-native';
// There is another version using the scroll component instead of animated.
// I'm unsure which one give the best UX. Please give us some feedback.
// import SwipeableViews from 'react-swipeable-views-native/lib/SwipeableViews.scroll';
const styles = StyleSheet.create({
slideContainer: {
height: 100,
},
slide: {
padding: 15,
height: 100,
},
slide1: {
backgroundColor: '#FEA900',
},
slide2: {
backgroundColor: '#B3DC4A',
},
slide3: {
backgroundColor: '#6AC0FF',
},
text: {
color: '#fff',
fontSize: 16,
},
});
const MyComponent = () => (
<SwipeableViews style={styles.slideContainer}>
<View style={[styles.slide, styles.slide1]}>
<Text style={styles.text}>
slide n°1
</Text>
</View>
<View style={[styles.slide, styles.slide2]}>
<Text style={styles.text}>
slide n°2
</Text>
</View>
<View style={[styles.slide, styles.slide3]}>
<Text style={styles.text}>
slide n°3
</Text>
</View>
</SwipeableViews>
);
export default MyComponent;
Documentation
Get started
Component API
More
(b). react-swipe
Swipe.js as a React component
Demo
Check out the demo from a mobile device (real or emulated).
Install
npm install react swipe-js-iso react-swipe --save
Usage
Examples
import React from 'react';
import ReactDOM from 'react-dom';
import ReactSwipe from 'react-swipe';
const Carousel = () => {
let reactSwipeEl;
return (
<div>
<ReactSwipe
className="carousel"
swipeOptions={{ continuous: false }}
ref={el => (reactSwipeEl = el)}
>
<div>PANE 1</div>
<div>PANE 2</div>
<div>PANE 3</div>
</ReactSwipe>
<button onClick={() => reactSwipeEl.next()}>Next</button>
<button onClick={() => reactSwipeEl.prev()}>Previous</button>
</div>
);
};
ReactDOM.render(<Carousel />, document.getElementById('app'));
Props
-
swipeOptions: ?Object
- supports all original options from Swipe.js config. If passed object differs from the previous onereact-swipe
will re-initiate underlying Swipe.js instance with fresh options -
style: ?Object
- object with 3 keys (see defaults):container: ?Object
wrapper: ?Object
child: ?Object
-
regular props as
className
,id
for root component are also supported -
childCount: ?Number
- use it to explicitely tellreact-swipe
that it needs to re-initiate underlying Swipe.js instance. For example, by setting thechildCount
prop to thelength
of the images array that you pass intoreact-swipe
, re-rendering will take place when theimages.length
differs from the previousrender
pass:
<ReactSwipe childCount={images.length}>{images}</ReactSwipe>
Methods
Component proxies all Swipe.js instance methods.