admin管理员组文章数量:1134586
I've just started exploring React, by adding a component with a simple render function:
render() {
return <div class="myApp"></div>
}
When I run the app, I get the following error:
Warning: Unknown DOM property class. Did you mean className?
I can solve this by changing class
to className
.
The question is; does React enforce this convention? Also why do I need to use className
instead of the conventional class
? If this is a restriction then is it due to JSX syntax or somewhere else?
I've just started exploring React, by adding a component with a simple render function:
render() {
return <div class="myApp"></div>
}
When I run the app, I get the following error:
Warning: Unknown DOM property class. Did you mean className?
I can solve this by changing class
to className
.
The question is; does React enforce this convention? Also why do I need to use className
instead of the conventional class
? If this is a restriction then is it due to JSX syntax or somewhere else?
8 Answers
Reset to default 217Yes, its a React convention:
Since JSX is JavaScript, identifiers such as
class
andfor
are discouraged as XML attribute names. Instead, React DOM components expect DOM property names likeclassName
andhtmlFor
, respectively.
JSX In Depth.
In React.js there is no for and class properties available so we need to use
for --> htmlFor
class --> className
Meteor uses babel to transpile ES5 to ES6 (ES2015), so we can deal with it as a normal Node application with babel transpiler added.
You need to add .babelrc
file to your project's root folder, and add the following items
{
"plugins": [
"react-html-attrs"
]
}
And of course, you need to install this plugin using npm
:
npm install --save-dev babel-plugin-react-html-attrs
In HTML we use
class="navbar"
but in React and ReactNative there is no HTML, we use JSX(Javascript XML) here we use
className="navbar"
You can't use class for any HTML tag attribute because JS has another meaning of class. That why you have to use className instead of class.
And also use htmlFor attribute instead of for.
In react 16.13.1 you can use class attribute. It throws a "warning" exception but it don'T stop the execution.
The JSX syntax when compiled using babel, underlying syntax it will use to create the HTML element will be
React.createElement('div', {attributes}, "Hello");
If we use the class instead of className, the react will infer it as javascript class instead of html class attribute. [Reason the variable name 'class' is already used in the file to create a javascript class and is reserved for the same purpose]. Which is wrong and compiler does throws the error, since javascript class is not a valid property for html DOM elements.
React.createElement("p", {"class": "header"}, "Hello");
Hence it is necessary to use className instead of class.
React.createElement("p", {"className": "header"}, "Hello")
I also encountered the same error when i used the tailwind CSS in the vite + react setup. But I fixed it by
METHOD 1) CHANGING THE FOLLOWING NAMING CONVENTIONS class --> className for --> htmlFor
METHOD 2) uninstalling the ESLint Extension from the vs code and the code works fine.
I hope it helps for more clearity read above answers of this thread
本文标签: javascriptWarning Unknown DOM property class Did you mean classNameStack Overflow
版权声明:本文标题:javascript - Warning: Unknown DOM property class. Did you mean className? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736779644a1952525.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论