question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React.cloneElement onClick does not work?

See original GitHub issue

Hi. Im trying to add an onClick handler to an already existing element.

Example:

render() {
    let element = <ImageCardView title="Test" subtitle="Subtitle" />;
    return React.cloneElement(element, { onClick: function() { console.log("TEST"); }});
}

// Result: Nothing happens when i click on the element

When setting it directly in the <ImageCardView /> it works.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:8

github_iconTop GitHub Comments

11reactions
ccorcoscommented, Aug 11, 2016

Why is this closed?

7reactions
abriginetscommented, Jul 11, 2021

Wow I’m so surprised this issue existed for so long without a clear explanation. Let me fill this gap for those who are going to search for it in the future. TLDR, it invokes props directly on your component, not the JSX you render

Let’s say we have a parent component:

export function ParentComponent(props) {
  return (
    <Wrapper>
      {React.cloneElement(
        props.children,
        {
           onClick: () => console.log('click'),
        },
      )}
    </Wrapper>
  );
}

And a child component:

export function ChildComponent(props) {
  return <button />;
}

And then we render it as following:

return (
  <ParentComponent>
    <ChildComponent />
  </ParentComponent>
);

What’s happening with child component is that React.cloneElement only adds onClick function to the list of props you are passing down to child prop. It does not invoke this prop in the JSX you declare. The solution here is as simple as it can be - just add onClick handler to the component you render:

export function ChildComponent(props) {
  return <button onClick={props.onClick} />;
}

It’s just the way our brain is messing with us and React have nothing to do with not handling clicks on your component 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Add events to cloned component - Stack Overflow
How can I add a click event to this cloned component and call a function within its class when clicked? I have tried...
Read more >
Using the React.cloneElement() function to clone elements
In this in-depth tutorial, learn how to clone and manipulate elements three different ways using the React.cloneElement function.
Read more >
Override or set property to React element - DEV Community ‍ ‍
This was usually a problem for me when building reusable components. ... The utility I am referring to is the cloneElement function exposed....
Read more >
How to use the react.cloneElement function in react - Snyk
cloneElement function in react. To help you get started, we've selected a few react examples, based on popular ways it is used in...
Read more >
cloneElement - React Docs
If you don't pass any ...children arguments, the original element.props.children will be preserved. Returns. cloneElement returns a React element object ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found