react-hammerjs icon indicating copy to clipboard operation
react-hammerjs copied to clipboard

Hammer swallowing child node ref

Open iDVB opened this issue 7 years ago • 7 comments

Was using...

     "react-dom": "^15.4.1",
     "react-hammerjs": "^0.5.0",

...and there was no issue. Now I'm using..

    "react-dom": "^16.1.0",
    "react-hammerjs": "^1.0.1",

...and suddenly all children nodes to Hammer are being ignored.

EG. in the following code thing1 consoles but thing2 does NOT.

    return (
      <div ref={thing1 => { console.log('thing1', thing1) }}>
        <Hammer
          onPan={e => this.pan(e, theta, panelCount)}
          options={hammerOptions}
        >
          <div ref={thing2 => { console.log('thing2', thing2) }}>
            <div>{panels}</div>
          </div>
        </Hammer>
      </div>
    );

...removing Hammer completely allows thing2 to console.

iDVB avatar Nov 20 '17 19:11 iDVB

Same problem here. In the meanwhile, you can add a ref to the direct parent of the <Hammer> component and then find the right node using different HTMLElement methods like querySelector or getElementsByClassName or such.

NordlingDev avatar Nov 24 '17 19:11 NordlingDev

+1, hope this gets fixed. @NordlingArt 's proposed workaround is a little messy

raulrene avatar Dec 21 '17 14:12 raulrene

I was frustrated by this and created a PR that fixes it. Hope @JedWatson has the time to go over it.

raulrene avatar Dec 21 '17 16:12 raulrene

+1 having the same problem here. Was working fine on v0.5.0.

Have worked around it as per NordlinArt's suggestion (add ref to parent of <Hammer>).

tc7 avatar Jan 30 '18 01:01 tc7

@raulrene's fix works, but it wasn't merged yet.

It seems that only the refs for the direct child node gets swallowed, so another workaround is to have an element wrapping your content inside the Hammer element, something like:

return (
  <div ref={thing1 => { console.log('thing1', thing1) }}>
    <Hammer
      onPan={e => this.pan(e, theta, panelCount)}
      options={hammerOptions}
    >
      <div>
        <div ref={thing2 => { console.log('thing2', thing2) }}>
            <div>{panels}</div>
        </div>
      </div>
    </Hammer>
  </div>
);

Of course, this works if you don't have some strict rules regarding your HTML structure.

adiulici avatar Feb 08 '18 07:02 adiulici

@adiulici thanks, this works, I have far stricter results about supporting multiple platforms with touch events, and having my child nodes update properly.

Cheers.

Pushplaybang avatar Mar 04 '18 17:03 Pushplaybang

@JedWatson If this project is abandoned? @raulrene's fix is still not merged...

alekseykarpenko avatar Dec 29 '19 18:12 alekseykarpenko