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

Avoid swallowing children's ref property, if it's set. Fixes #83 #87

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion dist/react-hammerjs.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ var HammerComponent = function (_React$Component) {

var self = this;
props.ref = function (domElement) {
if (self.props.ref) {
if (self.props.children && self.props.children.ref) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, whether this is the best solution because ref is a special prop.
Actually, the problem is not only that the child's ref is swallowed but also that self.props.ref will always return undefined (and I would expect the same for self.props.children.ref). A ref callback added to the <Hammer> component will still be executed (but independently, i.e. it will never be called here and if it did, it would be executed twice).

Maybe it is possible to add another prop to the official interface of the Hammer component that will be executed here. Something like

<Hammer
    onPan={onPanHandler}
    refCallback={domElement => myRef = domElement /* domElement will be the child, i.e. the div below */}
>
    <div>something cool</div>
</Hammer>

and then

props.ref = function(domElement) {
    if (self.props.refCallback) {
        self.props.refCallback(domElement);
    }
    self.domElement = domElement;
}

This would still swallow the child's ref but if it's documented that refCallback of the Hammer component will get passed the actual child element, one can just place the child's ref there and it will work as expected. This would also avoid trying to access the ref prop directly.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I second using a unique prop name such as refCallback as to avoid conflict with React special props.

// If the child has the ref set, call the child's ref method
self.props.children.ref(domElement);
} else if (self.props.ref) {
self.props.ref(domElement);
}
self.domElement = domElement;
Expand Down
5 changes: 4 additions & 1 deletion dist/react-hammerjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ var HammerComponent = function (_React$Component) {

var self = this;
props.ref = function (domElement) {
if (self.props.ref) {
if (self.props.children && self.props.children.ref) {
// If the child has the ref set, call the child's ref method
self.props.children.ref(domElement);
} else if (self.props.ref) {
self.props.ref(domElement);
}
self.domElement = domElement;
Expand Down
2 changes: 1 addition & 1 deletion dist/react-hammerjs.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion lib/Hammer.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ var HammerComponent = function (_React$Component) {

var self = this;
props.ref = function (domElement) {
if (self.props.ref) {
if (self.props.children && self.props.children.ref) {
// If the child has the ref set, call the child's ref method
self.props.children.ref(domElement);
} else if (self.props.ref) {
self.props.ref(domElement);
}
self.domElement = domElement;
Expand Down
Loading