Unable to prevent a touchend event from being canceled due to the event being non-cancelable, such as when scrolling is currently ongoing and cannot be disrupted

const SlideSettings = {
  dots: false,
  arrows: false,
  infinite: false,
  speed: 500,
  slidesToShow: 1,
  slidesToScroll: 1,
  draggable: true,
  // swipe: false,
  currentSlide: activeLocation,
  afterChange: (newIndex: number) => {
    setActiveLocation(newIndex);
  },
};

const SwipeSettings = {
  dots: false,
  arrows: false,
  infinite: false,
  speed: 500,
  slidesToShow: 1.5,
  slidesToScroll: 1,
  draggable: true,
  swipe:true
}; 

When working in NextJS, I found that I need to comment out the swipe option in order to use it. However, doing so triggers this error message:

Ignored attempt to cancel a touchend event with cancelable=false, for example because scrolling is in progress and cannot be interrupted.

Despite wanting to set swipe to true, I am unable to make it false. Is there a workaround for this issue?

Answer №1

Here are some suggestions to address this issue without disabling swipe functionality:

To prevent default touch behavior (like scrolling) during a swipe, you can add an event listener that stops the default action:
useEffect(() => {
  const handleTouchMove = (event) => {
    if (event.cancelable) {
      event.preventDefault();
    }
  };

  window.addEventListener('touchmove', handleTouchMove, { passive: false });

  return () => {
    window.removeEventListener('touchmove', handleTouchMove);
  };
}, []);

Modern browsers use passive event listeners for touch events for better performance. To avoid issues with canceling events, specify passive: false when adding the listener:
document.addEventListener('touchstart', (e) => {
  if (e.cancelable) {
    e.preventDefault();
  }
}, { passive: false });
Check if a touch event can be canceled before attempting to prevent it from happening:
const handleSwipe = (e) => {
  if (e.cancelable) {
    e.preventDefault();
    // Include custom swipe handling logic
  }
};
If the default swipe feature is causing problems, consider using a different library like react-swipeable to manage swipe events more effectively:
npm install react-swipeable
import { useSwipeable } from 'react-swipeable';

const handlers = useSwipeable({
  onSwipedLeft: () => console.log('Swiped left'),
  onSwipedRight: () => console.log('Swiped right'),
  preventDefaultTouchmoveEvent: true,
  trackMouse: true,
});

return <div {...handlers}>Try swiping here</div>;

By implementing these solutions, you can resolve the error while maintaining swipe functionality as desired.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Upon generating the app with npx create-react-app <myproject> and launching it with "npm start", the following error message was displayed:

An error occurred while using npm. The details are as follows: - Error code: ENOENT - Syscall: open - Path: E:\React js web\TextEdits/package.json - Errno: -4058 - Description: ENOENT: no such file or directory, open 'E:\React js web&bs ...

Modifying a JavaScript object causes changes to another object within the same array

Encountering an issue where player[0].pHand is being modified while updating player[1].pHand (where pHand is an array containing objects). for(var j = 0; j < 2; j++){ console.log(cardDeck[deckCounter]); player[0].pHand[j] = cardDeck[ ...

What is the inner workings behind server side rendering in Next.js?

I am seeking clarification on Server Side Rendering, specifically with Next.js. During server side rendering, I want to confirm the 'execution path' as follows: Client makes a request to the server for the webpage, which serves up an HTML only ...

retrieve an item that lacks a definitive value

Here's an object I have: Obj = { foo: false, bar: true, private: { something: 'else' } } Now, I'm trying to return this object without the private part. Since the private part is used elsewhere and cannot be spliced out, I ...

What's the best way to incorporate necessary styles into text using particular fonts?

Is there a way to set letter spacing and word spacing for text on my website with the font 'classylight'? Adding classes to each post in the site map seems like a lengthy process. To save time, I attempted to use the attribute*=style property to ...

A guide to incorporating a textview into a React application using the Google Maps API

Wondering how to incorporate a textview within a react-google-maps component? Successfully setting up a Google map page in React using the react-google-maps API, I've managed to insert markers and link them with polylines. import React from "react"; ...

Utilize useEffect to dynamically populate several dropdown menus with data

I am currently implementing a method to populate two dropdowns in my component using useEffects. function fetch_data_for_dropdown1() { return axios.get("http://127.0.0.1:5001/dropdownonedata"); } function fetch_data_for_dropdown2() { return axios ...

The communication between AJAX and PHP is not successful in transferring data

I'm struggling to find a solution for the issue I'm facing with accessing variables passed to my PHP script through AJAX. Even after using isset($_POST), I am unable to retrieve the username and password variables. Below is the AJAX call that I ...

Fetch a list of 10 items at a time using AngularJS with Ionic framework's infinite scroll feature

I'm currently working on incorporating an ion-infinite-scroll feature using the ionic framework. My REST API allows me to set the index in order to request a specific range of data. Here's what my Service looks like, with 'begin' and &a ...

How do you send a variable in a GET request with React?

My challenge is to retrieve data from a table where the teacherId matches the teacherId of the user who logs in, but I am facing difficulties in passing this teacherId from the front-end to the back-end. Below is the backend code: app.get("/api/get&q ...

How to access a variable using an HTML class in JavaScript?

Currently, I am delving into HTML and JavaScript code. Within the html, there exists the following line: <b>Project ID: <span class="project_id"></span></b> Therefore, class="project_id" contains a certain value that is displayed ...

The Bootstrap Navbar appears hidden beneath other elements on mobile devices

While using bootstrap to style my header contents, I encountered a strange issue. The navbar that appears after clicking on the hamburger menu is displaying behind all the components. Even though I've set the z-index to the maximum value, it still doe ...

Knockout Js' observable objects are not updating the UI to reflect changes in the data

Displayed below is the view modal where an ajax response is obtained and loaded into the observable value. var userManagementVM = { responseSetUpData: ko.observable({ userList: ko.observable(), userListViewModel: ko.observableArray(), ...

Updating Bootstrap 5.3 Modal to Have a Fixed Backdrop when Opened (Using JavaScript/JQuery)

Is there a way to change the backdrop of an open modal to 'static' using jQuery or JavaScript? I want to prevent the user from closing the modal by clicking outside after they have clicked the 'Submit' button on the modal. I've tri ...

In Vue.js, easily toggle classes with a click

I am having trouble figuring out how to apply a class to a button and then change it to another class after it is clicked. The idea is to have multiple buttons that serve as filters, allowing the user to select only one at a time with the selected button b ...

the display outcome appears fuzzy and lacks sharpness

Currently, I am engaged in prototyping and showcasing data in a 3D format using three.js (version 68). The intended outcome of the entire animation is to have a collection of colored spheres representing protons and neutrons, each colored based on a specif ...

What are the consequences of incorporating JavaScript in PHP code for changing locations?

There is a recurring question on Stack Overflow about redirecting users in PHP after input or values have been changed, and the common suggestion is to use headers for this task. However, it's important to note that headers in PHP need to be modified ...

How does the AngularJS Dependency Injection system determine the names of the arguments it needs to inject?

Here is an example directly from the official website: function PhoneListCtrl ($scope, $http) { $http.get('phones/phones.json').success(function(data) { $scope.phones = data; }); $scope.orderProp = 'age'; } The $s ...

How can I create 3 conditions in an AJAX request to verify a user's ID?

I am trying to create 3 conditions in Ajax to check when creating a new user id. It works for 2 conditions (username can be used and username is already in use), but I encounter a problem when the user leaves the username field empty, and it still displays ...

How does an arrow function access parameters even when they have not been explicitly passed in?

The PhotosPage component is being rendered with the following route: <Route path="/settings/photos" component={PhotosPage} /> The component's signature includes: const PhotosPage = ({ uploadProfileImage, photos, profile, deletePhoto, ...