React-slick: Drag Auto Trigger Click Event

Created on 20 Dec 2020  路  5Comments  路  Source: akiran/react-slick

Actively trigger a click event which I add on the img element when I drag the img element. I hope It can not trigger a click event when I drag, but the distance of dragging is small

Most helpful comment

hi @xunkaisummer , I was working on the same issue tonight and I think I found a solution using built-in slick functions. I had the same idea as @artuswo443 on issue #1956 to use onSwipe to detect when the swipe start. But there's no swipe end function. What I found that worked for me was to use afterChange to detect end of swipe.

Result here (twitter post)

Code:

  let isSwiping = false

  function updateSwipeState(state) {
    isSwiping = state
  }

  function handleClick() {
    if (isSwiping) {
      return
    }
    // do something onClick
  }

  return (
      <Slider  afterChange={ () => updateSwipeState(false) } onSwipe: { () => updateSwipeState(true) }>
        {
          posts.map(p => getBlogComponent(p, handleClick))
        }
      </Slider>
    </div>
  )
  1. create a isSwiping boolean.
  2. onSwipe updates isSwiping to true
  3. afterChange updates isSwiping to false
  4. in onClick add a condition to return if isSwiping is true

All 5 comments

Hi, @xunkaisummer, could you attach code snippet or video with reproduced problem.

Hi, @xunkaisummer, could you attach code snippet or video with reproduced problem.

hi @VadimSvirdoff , code is here, When I drag the img which I add click event on, It trigger a click event

`
import React, { Component } from 'react';
import Slider from 'react-animated-slider';
import 'react-animated-slider/build/horizontal.css';

const content = [
{
image: 'https://i.imgur.com/ZXBtVw7.jpg'
},
{
image: 'https://i.imgur.com/DCdBXcq.jpg'
},
{
image: 'https://i.imgur.com/DvmN8Hx.jpg'
}
];
class App extends Component {
render() {
return (



{content.map((item, index) => (
key={index}
style={{width: 100,height:100}}
onClick={() => {
console.log('click');
}}
>
{''}

))}


);
}
}

export default App;
`

hi @xunkaisummer you can set onClick and onMouseDown to div
live example:
codesandbox
or

  <div
      key={index}
      onMouseDown={(e) => handleOnMouseDown(e)}
      onClick={(e) => handleOnClick(e)}
  >

  const [ClientXonMouseDown, setClientXonMouseDown] = useState();
  const [ClientYonMouseDown, setClientYonMouseDown] = useState();
  const handleOnMouseDown = (e) => {
    e.preventDefault();
    setClientXonMouseDown(e.clientX);
    setClientYonMouseDown(e.clientY);
  };

  const handleOnClick = (e) => {
    e.stopPropagation();
    if (ClientXonMouseDown !== e.clientX || ClientYonMouseDown !== e.clientY) {
      e.preventDefault();
    }
  }; 

hi @xunkaisummer , I was working on the same issue tonight and I think I found a solution using built-in slick functions. I had the same idea as @artuswo443 on issue #1956 to use onSwipe to detect when the swipe start. But there's no swipe end function. What I found that worked for me was to use afterChange to detect end of swipe.

Result here (twitter post)

Code:

  let isSwiping = false

  function updateSwipeState(state) {
    isSwiping = state
  }

  function handleClick() {
    if (isSwiping) {
      return
    }
    // do something onClick
  }

  return (
      <Slider  afterChange={ () => updateSwipeState(false) } onSwipe: { () => updateSwipeState(true) }>
        {
          posts.map(p => getBlogComponent(p, handleClick))
        }
      </Slider>
    </div>
  )
  1. create a isSwiping boolean.
  2. onSwipe updates isSwiping to true
  3. afterChange updates isSwiping to false
  4. in onClick add a condition to return if isSwiping is true

seems like the lib need one more method processing this (actualy very popular) case =)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

slickGoTo as prop for PureComponent
briziel picture briziel  路  3Comments

appendArrows feature is not implemented yet
laveesingh picture laveesingh  路  3Comments

[Feature Request] - dotsStyle prop
PolGuixe picture PolGuixe  路  3Comments

Memory Leak
ramyatrouny picture ramyatrouny  路  3Comments

Installation instructions missing?
slashwhatever picture slashwhatever  路  3Comments