简体   繁体   中英

React component does not call onClick handler

I have a component built with es6 class syntax. It's a simple component with just a diverse and some styling. Then, on the parent component (called app), I'd like to render that component and give it an onClick that calls the parent's onclick handler. So far, the parent renders the child like so:

<child_component onClick={this.clickhandler}>

And then I have a method in that parent component that I have bound to the parent's context in the constructor.

The component renders, but the click handler is never called when I click the component. Any idea why?

Since child componet isn't a native DOM element but, another React component, onClick as defined here is simply a property of that component. What you need to do is, inside of the child component pass the onClick prop to an DOM component's onClick event:

var ChildComponet= React.createClass({
 render: function() {
 return (
   <div className="childcomponent" onClick={this.props.onClick}>
   ...
    </div>
  );
 }
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM