我的目标是有条件地向html元素添加id。在JSX中,我遇到了这样的难题:有条件地向html元素添加id:
<SomeHTMLElement id={condition ? "someId" : ""} />
或
<SomeHTMLElement id={condition ? "someId" : null} />
哪种方法是更好的HTML实践?默认是null
还是""
?
如果有另一种方法可以有条件地将id添加到HTML元素中,而不需要向元素本身引入id属性,那也是很好的方法。例如,<HTMLElement id />
或<HTMLElement id="" />
或<HTMLElement id={null} />
最好不要有空的id
属性,这样它就会编译成<HTMLElement />
https://stackoverflow.com/questions/56366659
复制相似问题