我什么时候应该在 'class' 上使用 'className',反之亦然?
When should I use 'className' over 'class' and vise versa in react?
我正在制作我的第一个 React 应用程序,我遇到了一些有趣的事情,我有时在 React 上使用 className
而不是 class
。我只是想知道它们是否可以互换,或者是出于某种原因这样吗?我还注意到,当我使用 class
时,inspect elements 控制台会询问我是否指的是 className
,没有错误,只是询问。
提前致谢!
这是识别未知 DOM 属性的另一个“功能”。
In the past, React used to ignore unknown DOM attributes. If you wrote JSX with an attribute that React doesn’t recognize, React would just skip it.
它 has changed 来自 React 16
并发出警告。您在 React 中使用 DOM 组件的方式没有改变,但现在您拥有了一些新功能。
React has always provided a JavaScript-centric API to the DOM. Since React components often take both custom and DOM-related props, it makes sense for React to use the camelCase convention just like the DOM APIs
至于className
:
To specify a CSS class, use the className attribute. This applies to all regular DOM and SVG elements like <div>
, <a>
, and others.
If you use React with Web Components (which is uncommon), use the class attribute instead.
参见Specifying Attributes with JSX:
Since JSX is closer to JavaScript than to HTML, React DOM uses camelCase property naming convention instead of HTML attribute names.
For example, class becomes className in JSX, and tabindex becomes tabIndex.
根据反应docs:
To specify a CSS class, use the className attribute. This applies to
all regular DOM and SVG elements like <div>
, <a>
, and others.
If you use React with Web Components (which is uncommon), use the
class attribute instead.
如果您使用 class
作为属性,它会在没有任何验证的情况下传递给基础 dom 元素,但这不是首选方法。 className
是 react 理解的属性。
React 文档建议使用 cannonical React attribute
名称而不是常规的 Javascript 命名,因此即使 React 允许将属性传递给 DOM,它也会给你一个警告.
我正在制作我的第一个 React 应用程序,我遇到了一些有趣的事情,我有时在 React 上使用 className
而不是 class
。我只是想知道它们是否可以互换,或者是出于某种原因这样吗?我还注意到,当我使用 class
时,inspect elements 控制台会询问我是否指的是 className
,没有错误,只是询问。
提前致谢!
这是识别未知 DOM 属性的另一个“功能”。
In the past, React used to ignore unknown DOM attributes. If you wrote JSX with an attribute that React doesn’t recognize, React would just skip it.
它 has changed 来自 React 16
并发出警告。您在 React 中使用 DOM 组件的方式没有改变,但现在您拥有了一些新功能。
React has always provided a JavaScript-centric API to the DOM. Since React components often take both custom and DOM-related props, it makes sense for React to use the camelCase convention just like the DOM APIs
至于className
:
To specify a CSS class, use the className attribute. This applies to all regular DOM and SVG elements like
<div>
,<a>
, and others.If you use React with Web Components (which is uncommon), use the class attribute instead.
参见Specifying Attributes with JSX:
Since JSX is closer to JavaScript than to HTML, React DOM uses camelCase property naming convention instead of HTML attribute names.
For example, class becomes className in JSX, and tabindex becomes tabIndex.
根据反应docs:
To specify a CSS class, use the className attribute. This applies to all regular DOM and SVG elements like
<div>
,<a>
, and others.If you use React with Web Components (which is uncommon), use the class attribute instead.
如果您使用 class
作为属性,它会在没有任何验证的情况下传递给基础 dom 元素,但这不是首选方法。 className
是 react 理解的属性。
React 文档建议使用 cannonical React attribute
名称而不是常规的 Javascript 命名,因此即使 React 允许将属性传递给 DOM,它也会给你一个警告.