I am trying to create a generic React component that I could wrap around any component and that would execute some code when it's clicked:
class WrapperComponent extends React.Component {
constructor() {
super();
this.state = {};
this.onElementClicked = this.onElementClicked.bind(this);
}
onElementClicked() {
// CODE TO BE EXECUTED WHEN WRAPPER ELEMENT IS LICKED
}
render() {
const {child_component}=this.props
return (
<span
onClick={() => {
this.onElementClicked();
}}
>
{child_component}
</span>
);
}
}
And this how I'd use it:
<WrapperComponent
component={<Button>Test</Button>}
></WrapperComponent>
This works perfectly fine.
However, I don't feel that it's right to follow this approach. This is what I would like to achieve:
<WrapperComponent>
<Button>Test</Button>
</WrapperComponent>
So that all I have to do is literally wrap whichever element I want with WrapperComponent
and achieve the same result.
Is that possible?