Inspiraller
Inspiraller

Reputation: 3806

How do I write an if statement to determine whether to display an attribute in jsx

This throws an error. Unexpected token for eachOption:

<input {eachOption===selectedValue?'checked="checked"':''} type="checkbox" name="checkme"/>

Upvotes: 0

Views: 912

Answers (1)

Elod Szopos
Elod Szopos

Reputation: 3526

Replace with checked={eachOption === selectedValue}.

That should do the trick.

However, make sure you also have the onChange handler attached to the checkbox (as any other input that you would like to use in React), otherwise it might not behave as you'd expect.

You should set values for attributes using JSX.

By setting the value of checked to false, it will mean that the checkbox will not be checked. If the condition (eachOption === selectedValue) holds then the checkbox will be checked for you.

Here's a JSBin

Upvotes: 4

Related Questions