gpt4 book ai didi

html - 我什么时候应该在 react 中使用 'className' 而不是 'class',反之亦然?

转载 作者:行者123 更新时间:2023-12-03 14:15:32 28 4
gpt4 key购买 nike

我正在制作我的第一个 React 应用程序,并且遇到了一些有趣的事情,我有时在 React 上使用 className 而不是 class。我只是想知道它们是否可以互换或者是有原因的吗?我还注意到,当我使用 class 时,检查元素控制台会询问我是否是 className,没有错误,只是询问。

提前致谢!

最佳答案

根据 react 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,它也会给你一个警告。

关于html - 我什么时候应该在 react 中使用 'className' 而不是 'class',反之亦然?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61596160/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com