[英]React : How to trigger a function on the "onClick" event, but on a component?
I try to trigger the handleClick
function when i click on my Trash
component, but it doesn't work because Trash
is not a DOM object.当我点击我的
Trash
组件时,我尝试触发handleClick
函数,但它不起作用,因为Trash
不是 DOM 对象。 How can i trigger my function when i click on this component ?单击此组件时如何触发我的功能?
const handleClick = () => {
console.log('OK');
}
return (
<div className="comment">
{trash && <Trash comment={comment} onClick={handleClick} />
</div>
);
You'll need to change your Trash component to accept an onClick
prop, and to pass it down to the returned JSX.您需要更改 Trash 组件以接受
onClick
道具,并将其传递给返回的 JSX。 Something like:就像是:
const Trash = (props) => {
// ...
return (
// whatever the top level element here is,
// add the onclick prop to it
<div onClick={props.onClick}>
// ...
</div>
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.