Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

React warns about for, class or stroke-width copied from HTML

Your error

Invalid DOM property `for`. Did you mean `htmlFor`?3 more lines it can show

Fix it fast

Most common cause, 1 of 3

for and class pasted from HTML

Rename the props.

Fix
<label htmlFor="email" className="field-label">Email</label>
<input id="email" name="email" type="email" />
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Invalid DOM property `for`. Did you mean `htmlFor`? means a JSX prop uses the HTML attribute name where React DOM expects the DOM property name: htmlFor for for, className for class. HTML or SVG spellings such as tabindex or stroke-width, typed or spread in, trigger the same warning. Rename each prop as suggested. Applies to React 18 and 19.

  • Applies to React 19.3, React 18.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Search the components for HTML spellings. A hit on for or class points to cause 1, a hit on another attribute to cause 2, and no hit at all while the warning keeps coming points to cause 3, a spread object. React 18 prints the same text with Warning: in front; React 19 drops that prefix.

Run this first
grep -rnE " (for|class|tabindex|readonly|maxlength|stroke-width|xlink:href)=" src/

Then find the line you see in this list and open its fix:

  1. 1for and class pasted from HTMLThe warning names for or class, and the search finds <label for=...> or class=... in a .jsx or .tsx file
  2. 2Other HTML or SVG attribute spellingsThe warning names tabindex, readonly, maxlength, autocomplete, stroke-width or xlink:href, often inside an icon or SVG pasted from a design tool
  3. 3The prop arrives through a spread objectThe search finds no for= in the file, but the element spreads an object such as {...labelProps} built from JSON, a CMS or a form schema

What does the error look like?

  • Invalid DOM property `for`. Did you mean `htmlFor`?
  • Warning: Invalid DOM property `for`. Did you mean `htmlFor`?
  • Invalid DOM property `class`. Did you mean `className`?
  • Invalid DOM property `stroke-width`. Did you mean `strokeWidth`?

Cause 1 of 3

for and class pasted from HTML

You seeThe warning names for or class, and the search finds <label for=...> or class=... in a .jsx or .tsx file

Check

Invalid DOM property `for` appears because React reads JSX props as DOM property names, and the property behind a label's for attribute is htmlFor, just as class becomes className. React DOM keeps a table of these standard names and warns when a prop equals one of them in lowercase but is spelled another way.

Check
grep -rnE " (for|class)=" src/

Fix

Rename the props. React still writes for and class into the HTML it renders, so each label stays linked to its input.

Fix
<label htmlFor="email" className="field-label">Email</label>
<input id="email" name="email" type="email" />

Verify

Reload the page in development: the warning is gone and the rendered label carries for="email".

Verify
document.querySelector('label[for="email"]') !== null

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

Other HTML or SVG attribute spellings

You seeThe warning names tabindex, readonly, maxlength, autocomplete, stroke-width or xlink:href, often inside an icon or SVG pasted from a design tool

Check

The same Invalid DOM property warning fires for any prop whose lowercase form is in React DOM's list of standard names while its spelling differs. That list maps tabindex to tabIndex, readonly to readOnly, maxlength to maxLength, autocomplete to autoComplete, stroke-width to strokeWidth and xlink:href to xlinkHref, which is why pasted SVG markup sets it off so often.

Check
grep -rnE " (tabindex|readonly|maxlength|autocomplete|stroke-width|xlink:href)=" src/

Fix

Use the name the warning suggests, in camelCase. ARIA attributes are the exception that keeps the HTML spelling, so aria-label stays aria-label.

Fix
<input tabIndex={0} readOnly maxLength={40} autoComplete="email" aria-label="Email" />

<svg viewBox="0 0 24 24" aria-hidden="true">
  <path d="M4 12h16" stroke="currentColor" strokeWidth={2} />
</svg>

Verify

After a reload the console has no Invalid DOM property line for the component.

Verify
grep -rnE " (tabindex|stroke-width|xlink:href)=" src/ | wc -l

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

The prop arrives through a spread object

You seeThe search finds no for= in the file, but the element spreads an object such as {...labelProps} built from JSON, a CMS or a form schema

Check

React validates the props a DOM element finally receives, not the text of the JSX, so a key named for inside an object spread onto <label> raises the same warning as typing it by hand. Form builders that store plain HTML attribute names in their config are the usual source.

Check
grep -rn "{\.\.\." src/components/Field.jsx

Fix

Rename the keys before spreading, in one small helper that every generated element uses.

Fix
const HTML_TO_REACT = { for: 'htmlFor', class: 'className', tabindex: 'tabIndex' };

function toReactProps(attrs) {
  return Object.fromEntries(
    Object.entries(attrs).map(([key, value]) => [HTML_TO_REACT[key] ?? key, value]),
  );
}

<label {...toReactProps(field.labelAttrs)}>{field.label}</label>

Verify

The helper returns htmlFor for a for key, and the console stays quiet when the form renders.

Verify
console.log(toReactProps({ for: 'email' })); // { htmlFor: 'email' }

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Keep attribute names that come from a CMS or a schema in one mapping helper, so a new field is renamed in one place.
  • When pasting SVG from a design tool, rename the hyphenated attributes in the same commit and leave aria-* names as they are.
  • Open the development console once after adding pasted markup; the warning names the exact prop and its React spelling.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, QistLock, Taskvare.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.