I would like to avoid manually adding the onClick property to my custom components.
For that, I thought about a Higher Order Component named WithClick that would wrap my components with the integrated onClick property.
The problem I'm facing is that in order to wrap it, I have to use an additional <div /> tag to access the event property. And this tag is messing up my CSS.
Example :
import React, { Component } from 'react'
const WithClick = (WrappedComponent) => {
class BaseComponent extends Component {
render () {
return (
<div onClick={this.props.onClick}>
<WrappedComponent {...this.props} />
</div>
)
}
}
return BaseComponent
}
export default WithClick
The solution would be a hack, allowing to attach an onClick event to a <React.Fragment /> tag or something similar.
I tried attaching ref to the child, but it doesn't work, I have to treat the ref prop in the child so there's no point doing that.
WrappedComponentlooks like as I am unsure why you do not want to pass onClick down to it. You still want the user to click a visible object don't you? Whether it's a thumbnail, a button, etc and those things have a layout at the end of the day.