1

I have on component which is stateless and it is used in both parent component

<div onClick={() => handleClick()}><div>

I have to add handle click function but only for one of them. I have one bool variable which is responsible for that e.g. shouldHandleClick.

So I need to write something like but it works for cases with props, strings etc. etc.

<div {shouldHandleClick ? onClick={() => handleClick()} : null}><div>

Do you have any ideas otherwise i will handle click in the second component which doesn`t need this handle click function

2 Answers 2

1

<div onClick={this.props.shouldHandleClick ? this.handleClick : null}><div>

And send shouldHandleClick with props to activate or deactivate click function.

Sign up to request clarification or add additional context in comments.

4 Comments

Be careful with context this
What wrong can be with context in ternary operator?
this.handleClick will be called from event listener. If you've not bound this directly to you handler it could cause problems.
I don`t have this in my case because this child component which handles the click is stateless. Off course, I bound this in the parent component. Btw everything works as expected - thanks for the syntax hints :))
1

It looks like you are almost there:

<div onClick={shouldHandleClick ? handleClick : null}></div>

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.