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

若如初见. 提交于 2019-12-25 06:34:31

问题


This throws an error. Unexpected token for eachOption:

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

回答1:


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



来源:https://stackoverflow.com/questions/38059192/how-do-i-write-an-if-statement-to-determine-whether-to-display-an-attribute-in-j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!