为了与 DOM 属性 API 保持一致,将 for
属性称为 htmlFor
。如果你正在使用 React 的开发版本,你应该已经在控制台中看到了关于此的警告。
是的,为了反应,
for
变为 htmlFor
class
变为 className
等等
在此处查看如何更改 HTML 属性的完整列表:
https://facebook.github.io/react/docs/dom-elements.html
对于 React,您必须使用它的自定义关键字来定义 html 属性。
类->类名
被使用并且
对于 -> htmlFor
使用,因为 react 区分大小写,请确保您必须根据需要遵循 small 和 capital。
for
和 class
都是 JavaScript 中的保留字,这就是为什么当涉及到 JSX 中的 HTML 属性时需要使用其他内容时,React 团队决定分别使用 htmlFor
和 className
for
是 JavaScript 中的保留字,因此 React 元素使用 htmlFor
。”:reactjs.org/docs/dom-elements.html#htmlfor
只是使用 react htmlFor 来替换 for!
因为 for 是 JavaScript 中的保留字,所以 React 元素使用 htmlFor 代替。
参考
您可以通过以下链接找到更多信息。
https://facebook.github.io/react/docs/dom-elements.html#htmlfor
https://github.com/facebook/react/issues/8483
https://github.com/facebook/react/issues/1819
那是 JSX 中的 htmlFor 和 class 是 JSX 中的 className
不定期副业成功案例分享
label
元素(由document.createElement
、document.getElementById
等返回),您可以将其for
属性作为label.htmlFor
访问。