React Easy Swipe
React Easy Swipe is a JavaScript library providing a simple component for handling touch swipe gestures within React applications. Currently stable at version 0.0.23, it offers straightforward integration for detecting common swipe directions (up, down, left, right) as well as general swipe start, move, and end events. The library differentiates itself by its minimal API and direct focus on touch interactions, with an option to enable mouse events for development. It includes features to prevent page scrolling during a swipe and to set a pixel tolerance for accidental swipes, making it suitable for mobile-first user interfaces. With its last publish date three years ago, releases appear to follow a 'when needed' cadence, characteristic of mature, focused utility libraries.
Common errors
-
Warning: Failed prop type: The prop `tolerance` is marked as required in `Swipe`, but its value is `undefined`.
cause The required `tolerance` prop was not provided to the `Swipe` component.fixAdd the `tolerance` prop with a number value, e.g., `<Swipe tolerance={10} ...>`. -
TypeError: Swipe is not a constructor (or similar errors like 'undefined is not a function')
cause Incorrect import statement used for the `Swipe` component, which is a default export.fixUse `import Swipe from 'react-easy-swipe';` instead of named imports like `import { Swipe } from 'react-easy-swipe';` or CommonJS `require` for ESM environments. -
Cannot read properties of undefined (reading 'x')
cause Attempting to access properties of the `position` object (e.g., `position.x`, `position.y`) in `onSwipeMove` when the event object might be structured differently or `position` is not yet defined in specific edge cases.fixEnsure `position` is defined before accessing its properties within the `onSwipeMove` handler, or add defensive checks if `position` can sometimes be `null` or `undefined`.
Warnings
- gotcha Swipes might interfere with page scrolling unless explicitly handled.
- gotcha By default, the component only responds to touch events, which can make testing on desktop challenging.
- breaking The `tolerance` prop is required and must be a number. Failing to provide it will result in a runtime error related to prop types.
- gotcha For accurate testing of touch-specific events, your browser must emulate touch gestures or you should test on a physical mobile device.
Install
-
npm install react-easy-swipe -
yarn add react-easy-swipe -
pnpm add react-easy-swipe
Imports
- Swipe
import { Swipe } from 'react-easy-swipe'; const Swipe = require('react-easy-swipe');import Swipe from 'react-easy-swipe';
- SwipeEvent
import Swipe, { SwipeEvent } from 'react-easy-swipe';
Quickstart
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import Swipe from 'react-easy-swipe';
class MyComponent extends Component {
onSwipeStart(event) {
console.log('Start swiping...', event);
}
onSwipeMove(position, event) {
console.log(`Moved ${position.x} pixels horizontally`, event);
console.log(`Moved ${position.y} pixels vertically`, event);
// Return true to prevent scroll during swipe
// return true;
}
onSwipeEnd(event) {
console.log('End swiping...', event);
}
render() {
const boxStyle = {
width: '100%',
height: '300px',
border: '1px solid black',
background: '#ccc',
padding: '20px',
fontSize: '3em',
display: 'flex',
alignItems: 'center',
justifyContent: 'center'
};
return (
<Swipe
onSwipeStart={this.onSwipeStart}
onSwipeMove={this.onSwipeMove}
onSwipeEnd={this.onSwipeEnd}
allowMouseEvents={true} // Enable for desktop testing
tolerance={15} // Pixel tolerance for accidental swipes
>
<div style={boxStyle}>Open the console and swipe me</div>
</Swipe>
);
}
}
const rootElement = document.getElementById('root');
if (rootElement) {
ReactDOM.render(<MyComponent />, rootElement);
} else {
console.error('Root element not found. Please ensure an element with id="root" exists in your HTML.');
}