166

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?

Bono
  • 4,757
  • 6
  • 48
  • 77
jsalonen
  • 29,593
  • 15
  • 91
  • 109

7 Answers7

215

Yes, its a React convention:

Since JSX is JavaScript, identifiers such as class and for are discouraged as XML attribute names. Instead, React DOM components expect DOM property names like className and htmlFor, respectively.

JSX In Depth.

Edgar
  • 6,022
  • 8
  • 33
  • 66
Viacheslav
  • 3,876
  • 2
  • 21
  • 28
  • 42
    I find this terribly unintuitive. I think the parser should know how to switch contexts between the two languages based on context, keep the burden off the programmer and on the tools – Jonathan Jan 29 '19 at 18:37
  • 1
    Agreed this should be in the tooling, though `class` is a reserved keyword in pretty much every programming language and its quite easy doing Find `class=`, Replace `className=` – Jeremy Thompson Jan 16 '22 at 11:39
23

In React.js there is no for and class properties available so we need to use

for   --> htmlFor
class --> className
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
16

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

jsalonen
  • 29,593
  • 15
  • 91
  • 109
securecurve
  • 5,589
  • 5
  • 45
  • 80
12

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"
Muhammad Talha
  • 379
  • 3
  • 6
5

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.

Nimmi Verma
  • 465
  • 6
  • 11
4

In react 16.13.1 you can use class attribute. It throws a "warning" exception but it don'T stop the execution.

enter image description here

kodmanyagha
  • 932
  • 12
  • 20
2

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")
Sasi Kumar M
  • 2,440
  • 1
  • 23
  • 23