React JS onClick event handler

前端 未结 11 1146
孤街浪徒
孤街浪徒 2020-11-28 21:35

I have

var TestApp = React.createClass({
      getComponent: function(){
          console.log(this.props);
      },
      render: function(){
        retur         


        
11条回答
  •  时光说笑
    2020-11-28 22:02

    Handling events with React elements is very similar to handling events on DOM elements. There are some syntactic differences:

    • React events are named using camelCase, rather than lowercase.
    • With JSX you pass a function as the event handler, rather than a string.

    So as mentioned in React documentation, they quite similar to normal HTML when it comes to Event Handling, but event names in React using camelcase, because they are not really HTML, they are JavaScript, also, you pass the function while we passing function call in a string format for HTML, they are different, but the concepts are pretty similar...

    Look at the example below, pay attention to the way event get passed to the function:

    function ActionLink() {
      function handleClick(e) {
        e.preventDefault();
        console.log('The link was clicked.');
      }
    
      return (
        
          Click me
        
      );
    }
    

提交回复
热议问题