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

Passing parameters on :onclick #46

Closed
MasterPuffin opened this issue Sep 2, 2024 · 1 comment
Closed

Passing parameters on :onclick #46

MasterPuffin opened this issue Sep 2, 2024 · 1 comment

Comments

@MasterPuffin
Copy link

Currently if I want to pass a parameter on an onclick Event, I have to call it inside an arrow function like this

<button :onclick="()=>{edit(area.id)}">Edit</button>

however it would be nice, if I could call it just like this

<button :onclick="edit(area.id)">Edit</button>
@dy
Copy link
Owner

dy commented Sep 2, 2024

Yes, it was deliberate choice as result of research. Previous versions had @click="edit(area.id)" style of event handlers, similar to vue / alpine / lit / etc, but as it turned out, that's quite limiting and not as clear.

Few reasons why :onclick="event => {...}" is a better choice:

  • It gives explicit event argument, allowing to avoid implicit magic like $event or event etc
  • It is more consistent with el.onclick = handler in JS. All other attributes set value like that :x="y"
  • It makes obvious that it's function body that permits let, const etc. :onclick="const x = 1;" is confusing, :onclick="()=>{const x = 1;}" is not.
  • Vue/alpine has ambiguity where :onclick="handler()" is identical to :onclick="handler", which is confusing.
  • It allows events chain syntax :onfocus..onblur="() => (on(), () => off())".

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

2 participants