0

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. 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>
  );
2
  • I'm not sure I understand. Whatever the "Trash" component is needs to have something in it that's clickable and calls the handler. Commented Nov 6, 2021 at 15:24
  • Can you share the Trash component? Without that code, we can't understand why handleClick isn't being called. Commented Nov 6, 2021 at 16:01

1 Answer 1

2

You'll need to change your Trash component to accept an onClick prop, and to pass it down to the returned 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>
  );
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.