Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Onchange event on Select box triggers twice #160

Closed
chiragmongia opened this issue Aug 30, 2016 · 6 comments
Closed

Onchange event on Select box triggers twice #160

chiragmongia opened this issue Aug 30, 2016 · 6 comments
Labels

Comments

@chiragmongia
Copy link
Contributor

The onchange event on my select box triggers twice.
By debugging it, I could find that in this line, there are 2 values being passed to multihook function and both the values are same i.e. onchange

@developit Can you please help?
If the above is the case and it's a bug, I'll be more than happy to contribute 😄

@developit
Copy link
Member

@chiragmongia Ahh - you're seeing applyEventNormalization() called on the same VNode twice? If so that seems like a bug. Maybe we have that function add a hint indicating that it's already transformed a given VNode?

@developit developit added the bug label Aug 30, 2016
@chiragmongia
Copy link
Contributor Author

@developit Yes, you're right. So, I created a small jsfiddle to see what's happening.
In this fiddle, the event triggers only once. But when I remove /** @jsx h */ from the file, it starts triggering twice 😲
Can you help me understand what's happening here? 😕

@developit
Copy link
Member

Ah yes - setting the JSX pragmatic to "h" actually bypasses preact-compat's VNode normalization. Since that's where the bug is, it only fires once. The default JSX pragma is React.createElement.

@chiragmongia
Copy link
Contributor Author

@developit Thanks for the help.
I'm using grunt-browserify. In Gruntfile.js, the configuration was-

[ "babelify",
    {
        'presets':['es2015', "react"]
    }
]

I've changed it to the following and it worked-`

[ "babelify",
    {
        'presets':['es2015', "react"],
        "plugins": [
            ["transform-react-jsx", {
                "pragma": "h"
            }]
        ]
    }
]

Now, the onChange event triggers once only.
PS - The same babelify configuration exists in .babelrc file as well. I'm not sure if I need that file anymore.
Thanks again! 👍

@developit
Copy link
Member

Great! I'll probably do a PR to fix the double call still, but I'm glad it's working for you.

Only thing to be aware of is preact's h() does not support string refs, only function refs.

@chiragmongia
Copy link
Contributor Author

chiragmongia commented Aug 31, 2016

@developit The applyEventNormalization() is called once on the VNode but in this function, there is an if condition to check if nodeName is select, then it adds another onchange attr on it.
Removing this condition fixes the issue.
I'm raising a PR. Please check if it seems fine to you.

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

No branches or pull requests

2 participants