Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't Nest React Components Within Hammer #91

Open
choxi opened this issue Apr 3, 2018 · 2 comments
Open

Can't Nest React Components Within Hammer #91

choxi opened this issue Apr 3, 2018 · 2 comments

Comments

@choxi
Copy link

choxi commented Apr 3, 2018

I'm trying to nest another React component within the Hammer component:

import React from "react"
import Hammer from "react-hammerjs"

class Note extends React.Component {
  render() {
    let style = { height: "50px", width: "50px", background: "blue" }

    return <div className="Note" style={ style } >
      { this.props.name }
    </div>
  }
}

export default class App extends React.Component {
  handleTap(...args) {
    alert("tap")
  }

  render() {
    return <Hammer onTap={ (...args) => this.handleTap(...args) }>
      <Note />
    </Hammer>
  }
}

... and I get this error:

image

It works fine unless I use another React component inside of <Hammer>.

@storrdev
Copy link

I believe I've found a possible work-around for now.

You will need to add the following method to your nested React component:

addEventListener(type, handler, useCapture) {
    this.el.addEventListener(type, handler, useCapture);
}

and make sure to add the ref to the nested component's root element:

ref={el => this.el = el}

I haven't tested it thoroughly, but it's working for me so far.

@jameschetwood
Copy link

@storrdev You can just put a div between Hammer and the React component, see here:
mosch/react-avatar-editor#132

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants