Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React, How to prevent execution of click event after drag?

I have Draggable element and inside of it, I have a component with onClick event. At the end of the drag, the click event is triggered. My draggable element looks like this. I used a package called react-draggable.

<Draggable
    position={this.state.realPosition}
    onStart={this.handleStart}
    onDrag={this.handleDrag}
    onStop={this.handleStop}
    disabled={this.state.isDialogOpen}
    bounds="parent">
      <div style={{"width":"fit-content"}}>
        <Helmet getDialogStatus={this.handleClick} />  
      </div>
</Draggable>

I have onClick event inside of the Helmet component which opens a dialog box. When I drag and release the element, this dialog opens. My question is how to prevent this action and how to seperate these to event?

Thanks.

like image 789
İbrahim Akar Avatar asked Oct 30 '25 10:10

İbrahim Akar


1 Answers

const [isDragging, setIsDragging] = useState<any>(false);

const eventControl = (event: { type: any; }, info: any) => {


    if (event.type === 'mousemove' || event.type === 'touchmove') {
      setIsDragging(true)
    }

    if (event.type === 'mouseup' || event.type === 'touchend') {
      setTimeout(() => {
        setIsDragging(false);
      }, 100);

    }
  }

and then ,

<Draggable
    bounds="parent"
    onDrag={eventControl}
    onStop={eventControl}
> ...
like image 151
lakjeewa Wijebandara Avatar answered Nov 01 '25 02:11

lakjeewa Wijebandara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!