1

So I have this button in JSX:

<button className="btn btn-primary" {usersOnCall.indexOf(user) > -1 ? "disabled" : ""}></button>

I want the button to be disabled when the conditional is true, I tried above code but it didn't work. Can anybody give me a suggestion how to do it? Many thanks!

1
  • 2
    you are not adding disable as attribute, that is your problem. Commented Sep 5, 2017 at 9:25

3 Answers 3

4

Change your code accordingly

<button className="btn btn-primary" disabled={usersOnCall.indexOf(user) > -1 ? true : false}></button>

Or you can shorten with

<button className="btn btn-primary" disabled={(usersOnCall.indexOf(user) > -1)}></button>

Update accourding to @Chris's comment

ES6 option:

<button className="btn btn-primary" disabled={usersOnCall.includes(user)}></button>
Sign up to request clarification or add additional context in comments.

2 Comments

This is the best option.
For ES6 I would also add disabled={usersOnCall.includes(user)} as an option.
1

Use disabled as a prop as well as className. usersOnCall.indexOf(user) > -1 is already a boolean value and you don't have to return true or false

<button
  className="btn btn-primary"
  disabled={usersOnCall.indexOf(user) > -1}
></button>

Comments

0

Place your condition instead of YOUR_CONDITION.

<button
  disabled={YOUR_CONDITION}>
</button>

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.