0

Well, I have a react component in REACT JS which would be perfect for reusing. But it is working with 'onDoubleClick' event listener currently. I would like to reuse it with a simple 'onClick' event listener (without I clone this comp). Which event listener should be called depends on where the component is in the application. More precisely, the choice of event listener is depending on the parents of the component.

Any idea, how can I do that? ...or I have to clone the original comp? :)

Thanks in advance for your help

Crepkey
  • 386
  • 1
  • 4
  • 8

1 Answers1

0

By adding both into your reusable component I guess (I have not, and can't test it currently), however, I'm reply based on my similar experience using both onPress and onLongPress and the post is here:

const { onClick, onDoubleClick } = this.props;

<YourResuableComponent 
  onClick={onClick? onClick : () => {}} 
  onDoubleClick={onDoubleClick? onDoubleClick : () => {}} 
/>

And say you have

handleClick = () => {
  console.log('click')
}

you simply call your component like this:

<YourResuableComponent onClick={this.handleClick} />

However, if it doesn't work, I guess you might want to read this

Andus
  • 1,713
  • 13
  • 30
  • It works very well, thank you so much the prompt answer and your great help. Additional information: even the ternary operator was not necessary in my case. – Crepkey Jan 10 '20 at 11:00