1

I have onClick that I want to hit a certain function depending on the following logic:

onClick={ approve && this.handleApproveClick, !approve && !releaseHold && this.handleDeclineClick, releaseHold && this.handleReleaseHoldClick } 

Oddly enough the last this.handleReleaseHoldClick works, while the others do not. What is the correct way to do this? Or do I really need to create a separate button?

1 Answer 1

1

Why does only the last work?

It's a basic comma operator case, where the "comma operator evaluates each of its operands (from left to right) and returns the value of the last operand".

What is the correct way to do this?

Since it's a triple-condition function, I'd suggest you to create a class method and simply secure each of possible cases.

handleSomeClick = () => { if (approve) { this.handleApproveClick(); } else if (!approve && !releaseHold) { this.handleDeclineClick(); } else if (releaseHold) { this.handleReleaseHoldClick(); } } 

and inside JSX:

onClick={this.handleSomeClick} 
Sign up to request clarification or add additional context in comments.

1 Comment

You are without a doubt, the kindest user i've ever seen.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.