onClick=“” don't appear at the actual element made with react.

拟墨画扇 提交于 2021-01-28 00:31:31

问题


I'm making some elements with react like this:

var AddNewTask = React.createClass({
  handleClick: function() {
   console.log('pressed!');
  },
  render: function() {
    return (
      <div className="addNewTask" onClick={this.handleClick}>
        <i className="material-icons">add</i>
      </div>
    );
  }
});

but on the actual rendered html page this div does not contain onClick property. Just looks like this.

<div class="addNewTask" data-reactid=".0.2">
  <i class="material-icons"  data-reactid=".0.2.0">add</i>
</div>

How to make it appear?


回答1:


It won't appear in the rendered HTML, as the handler is attached to the DOM element itself. React also normalizes the event system across browsers so that it behaves consistently. I would suggest reading up on how it works at the official docs: https://facebook.github.io/react/docs/events.html




回答2:


Simply put:

  1. onClick on jsx is not DOM-onClick-event but React-onClick-event.
  2. React-onClick-event is belong to React, so it's not displayed on DOM.
  3. React.js automatically convert React-onClick-event into DOM-onClick-event behind the scene.


来源:https://stackoverflow.com/questions/34913296/onclick-dont-appear-at-the-actual-element-made-with-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!