日本語

React Invalid DOM property warning: fixing class and for

​​React​Troubleshooting,TypeScript,Vite

The warning Invalid DOM property `class`. Did you mean `className`? appears in React when JSX uses class, the HTML spelling. Changing class to className, and for on a <label> to htmlFor, removes the warning. It appears when HTML templates or design samples are pasted into JSX unchanged.

I caused this warning on purpose in a test environment, and confirmed the text shown in the browser console and that the warning goes away after the fix. I also checked what the page looked like while the warning was showing.

Note

The test environment was Ubuntu 24.04 with Node.js 22.23.4. I used a project made with the react-ts template of npm create vite@latest (react and react-dom 19.3.1, Vite 8.3.8, TypeScript 7.0.2, oxlint 1.93.0), and wrapped the components in StrictMode. I opened the pages in Chromium 153 driven by Playwright 1.63.0. The DevTools screenshots were taken from a headed Chromium running on a virtual display (Xvfb). I did not test development servers of frameworks such as Next.js, or display in browsers other than Chromium.

The message when written as class

I opened the following component with the development server (vite, as in npm run dev). The <ul> and <li> use class, written the same way as in HTML.

tsx
// Wrong: written as class, as in HTML
const cats = ['たま', 'みけ', 'くろ'];

export default function BadClass() {
  return (
    <ul class="cat-list">
      {cats.map((cat) => (
        <li key={cat} class="cat-item">
          {cat}
        </li>
      ))}
    </ul>
  );
}

The DevTools console showed this line:

text
Invalid DOM property `class`. Did you mean `className`?

This means that class is not a valid DOM property and that className was probably meant. The message includes both the wrong name and the name to use, so following it fixes the code.

DevTools console showing one red line: "Invalid DOM property `class`. Did you mean `className`?" Above it, the page shows a list of three lines: "Tama", "Mike", and "Kuro". The names are in Japanese in the screenshot.
The page with class, after opening it. The list is displayed, and one red line appeared in the console

The warning appears as a red error line, not a yellow warning, because react-dom's development build reports it with console.error.

This code writes class in two places (<ul> and <li>), and rendering it puts the attribute on four elements. Still, only one message appeared. Reading the react-dom 19.3.1 development build, I found that it remembers each property name it has already warned about and does not warn again for the same name while the page stays open. So one message does not mean class was written in only one place. The message text also does not include a file name or component name. The reliable way to fix it is to search the whole project for class= and for= and fix every match.

Also shown for label's for and SVG attributes

I also tested for on a <label>.

tsx
// Wrong: for written on label
export default function BadFor() {
  return (
    <p>
      <label for="email">メールアドレス</label>
      <input id="email" type="email" />
    </p>
  );
}
text
Invalid DOM property `for`. Did you mean `htmlFor`?

When an SVG attribute was written with a hyphen, the message had the same form.

tsx
// Wrong: SVG attribute written with a hyphen
export default function BadSvg() {
  return (
    <svg width="40" height="40" viewBox="0 0 40 40">
      <circle cx="20" cy="20" r="16" fill="none" stroke="#00c8e8" stroke-width="4" />
    </svg>
  );
}
text
Invalid DOM property `stroke-width`. Did you mean `strokeWidth`?

The react-dom code contains a list of correct names (for example, class maps to className, for to htmlFor, and stroke-width to strokeWidth). This message appears when a name you wrote is in that list but written in the wrong form.

The official JSX documentation says to write strokeWidth instead of stroke-width, and to write className because class is a reserved word in JavaScript. The same section says that only aria-* and data-* attributes keep their hyphens, as in HTML.

The page still worked despite the warning

When I inspected the page with the warning, class, for, and stroke-width were all present as HTML attributes. This is the content of #root on the page written with class:

html
<ul class="cat-list"><li class="cat-item">たま</li><li class="cat-item">みけ</li><li class="cat-item">くろ</li></ul>

It matched the page fixed with className character for character. On the page written with for, clicking the label text moved focus to the input field.

So the warning did not change the appearance or behavior of these pages. I still fix it, because the TypeScript type check fails on it (explained in a later section). Also, a red line that stays in the console makes other errors harder to notice.

Replacing with className and htmlFor removes it

The official reference for props accepted by DOM components lists className and htmlFor. The htmlFor entry says it is the same as the HTML for attribute, and that React uses the DOM property name, not the HTML attribute name.

tsx
// Fixed: class changed to className
const cats = ['たま', 'みけ', 'くろ'];

export default function GoodClass() {
  return (
    <ul className="cat-list">
      {cats.map((cat) => (
        <li key={cat} className="cat-item">
          {cat}
        </li>
      ))}
    </ul>
  );
}
tsx
// Fixed: for changed to htmlFor
export default function GoodFor() {
  return (
    <p>
      <label htmlFor="email">メールアドレス</label>
      <input id="email" type="email" />
    </p>
  );
}
tsx
// Fixed: hyphenated attribute changed to camelCase
export default function GoodSvg() {
  return (
    <svg width="40" height="40" viewBox="0 0 40 40">
      <circle cx="20" cy="20" r="16" fill="none" stroke="#00c8e8" strokeWidth="4" />
    </svg>
  );
}

When I opened the three fixed pages with the same development server, the console showed no error lines.

The fixed page shows a list of three lines: "Tama", "Mike", and "Kuro". The names are in Japanese in the screenshot. Below it, the DevTools console shows only one line, a note from React DevTools.
The page after changing to className, after opening it. No red line appeared

The one line left in the console is a note from React DevTools. It also appeared on the pages before the fix and is unrelated to the warning.

The TypeScript type check catches class and for

When I ran npx tsc -p tsconfig.app.json --noEmit in the test project, class and for were reported as errors.

text
src/BadClass.tsx(6,9): error TS2322: Type '{ class: string; children: Element[]; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLUListElement>, HTMLUListElement>'.
  Property 'class' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLUListElement>, HTMLUListElement>'. Did you mean 'className'?
src/BadClass.tsx(8,23): error TS2322: Type '{ key: string; class: string; children: string; }' is not assignable to type 'DetailedHTMLProps<LiHTMLAttributes<HTMLLIElement>, HTMLLIElement>'.
  Property 'class' does not exist on type 'DetailedHTMLProps<LiHTMLAttributes<HTMLLIElement>, HTMLLIElement>'. Did you mean 'className'?
src/BadFor.tsx(5,14): error TS2322: Type '{ for: string; children: string; }' is not assignable to type 'DetailedHTMLProps<LabelHTMLAttributes<HTMLLabelElement>, HTMLLabelElement>'.
  Property 'for' does not exist on type 'DetailedHTMLProps<LabelHTMLAttributes<HTMLLabelElement>, HTMLLabelElement>'. Did you mean 'htmlFor'?

Unlike the browser message, the type errors list every place where class is written, with the file name and line and column. The template's npm run build runs tsc first, so this error stops the build before the production bundle is created.

BadSvg.tsx, which uses stroke-width, passed the type check. This warning appears only in the browser console, so when you paste SVG code, check the console with the development server. I also ran oxlint, the lint tool included in the template, with its settings unchanged, using npx oxlint src. It reported nothing for any file.

As the Vite documentation states, the development server does not run type checks. Type errors can be missed in the editor, and the page still opens normally.

I skipped the type check and built for production with only npx vite build, then opened the result with npx vite preview. The page written with the wrong names still showed nothing in the console. The production build of react-dom does not include the text of this message, so check the warning with the development server.

​Posted by 86