can we attach click handlers to custom child components

前端 未结 2 2226
灰色年华
灰色年华 2020-12-18 22:57

I was trying to add a click handler to my own child component. In react chrome extension I was able to see the click handler as well.

But the click itself didn\'t wo

2条回答
  •  长情又很酷
    2020-12-18 23:15

    You can add the handler from the samecomponent or call it through props. Below code looks for onClick param in props. If nothing is passed, then it goes for default handler in the component(clickHandler).

    var MySampleComponent = React.createClass({
      clickHandler: function(){
           // write your logic
      },
      render: function() {
        return 
    ...
    ; } });

    and while using this in another component use it as below

    ...........
    handler: function() {
       // write your logic 
    },
    render {
      var self = this;
      return ();
     }
    
    ......
    

提交回复
热议问题