最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML event handler vs React event handler - Stack Overflow

programmeradmin2浏览0评论

I would like to ask a question about event handlers when used in HTML and React.

In the book Javascript and Jquery written by Jon Duckett, the author mentioned that using HTML event handler attribute is considered bad practice for instance something like the following:

<button onClick="hide()">click me</button>

But recently I am starting to learn React and when a ponent is defined, there are many examples of event handlers used as an attribute and it seems mon to do so, not getting criticism for doing so,

<button onClick={doSomething}>
  xxxyyyzzz
</button>

Is there a reason to this? is it because this is the only way to bind a event handler in React?, from the way I see it React is essentially building the ponent element via HTML but uses the event handler attribute to assign an event to it, so why is React bringing back a concept that is considered bad practice?

I would like to ask a question about event handlers when used in HTML and React.

In the book Javascript and Jquery written by Jon Duckett, the author mentioned that using HTML event handler attribute is considered bad practice for instance something like the following:

<button onClick="hide()">click me</button>

But recently I am starting to learn React and when a ponent is defined, there are many examples of event handlers used as an attribute and it seems mon to do so, not getting criticism for doing so,

<button onClick={doSomething}>
  xxxyyyzzz
</button>

Is there a reason to this? is it because this is the only way to bind a event handler in React?, from the way I see it React is essentially building the ponent element via HTML but uses the event handler attribute to assign an event to it, so why is React bringing back a concept that is considered bad practice?

Share Improve this question asked Aug 17, 2017 at 8:47 securenovasecurenova 5022 gold badges9 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

Why are inline event listeners bad practice?

HTML describes the content and structure of a document, CSS describes the appearance and JavaScript is about the logic or behavior. These things should be kept separately. HTML and JavaScript code shouldn't be mixed. Your example for that was:

<button onClick="hide()">click me</button>

This, however, does not apply to React: we don't have one HTML file. Instead, we have modularized ponents, which have their own structure, style and behavior.

In React, we don't want the separation of presentation and logic, but self-contained ponents. That's what sets it apart from an application using just "Vanilla JavaScript" / the DOM API.

In the previous era of Javascript, this is considered as bad practice because we want to seperate HTML and JS as much as possible for better code management. You cannot quickly navigate to your onClick within an HTML page with another bunch of button.

And here in React, you manage your application code through the ponent tree. I think the power of React are:

  • Modularity
  • Composition
  • DRY (don't repeat yourself)

Back to your simple example, the onClick handler will be easily managed inside your ponent. Maybe with some other handler as well but it still be convenient within the scope of 1 ponent. Depend on which level of your ponent hierachy, everything you want to do lies on the same page, same class, same function.

发布评论

评论列表(0)

  1. 暂无评论