Jump to content
Search Community

Draggable not working with React 16.5

DraggableSock test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

First of all, congrats for all stuff GSAP! This is a wonderful piece of work!

Secondly, I've encountered issues with Draggable, while using React 16.5:
Using Draggable.create(target) doesn't allow the user to drag the element at all. A 3d transform is set on the element, having all 0px values, but the element itself doesn't move.
The onDrag / onDragEnd / onDragStart handlers aren't called. On the other hand, the onPress handler is called, but two times every click, once with a PointerEvent type event payload, and with a MouseEvent, the second time.


Also, it's important to point out that using GSAP 1.19.1 / 2.0.0 / 2.0.2 with React 16.4 and lower doesn't reproduce the issue, nor does it cause the onPress handler to be called twice. It only gets called once, with a PointerEvent.
However, I was able to reproduce this issue using GSAP 1.19.1 / 2.0.0 / 2.0.2, with React 16.5.

If there's anything more needed, I'll happily provide more details.
 

  componentDidMount() {
    const lis = document.getElementsByTagName('li');

    Draggable.create(lis, { 
      cursor: 'grab',
      onPress(event) { console.log(event); },
      onDragEnd() { 
        TweenMax.to(this.target, 0.5, { x: 0, y: 0 }); 
      } 
    });
  }


Thanks in advance!

Link to comment
Share on other sites

Yeah, sorry, it's actually a problem that was introduced in React 16.5 because they added an onclick handler on the root element, thus Draggable is trying to respect that as a "clickable" element (so not draggable). The fix should be as easy as adding this to your Draggable:

 

dragClickables:true

 

We're planning to make that the default in the next release of Draggable anyway, FYI. 

 

Does that help? 

 

  • Like 4
Link to comment
Share on other sites

  • 4 years later...
3 minutes ago, Seiwa said:

This doesn't work for me. When I make changes and I run it for the 1st time it works but after refresh it doesn't work.

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or Stackblitz that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Please read this article: 

 

You're not doing proper cleanup nor are you structuring things properly in React (this has nothing to do with GSAP/Draggable - it's all React stuff). You should put things in a useLayoutEffect() to ensure it runs after the render so that the elements actually exist. Right now, you were trying to grab an element that didn't even exist at that point. 

 

I assume you were looking for something like this?: 

https://stackblitz.com/edit/gsap-react-basic-f48716-dpacqw?file=src%2FApp.js

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...