react
runtime_error
ai_generated
true
Warning: Invalid DOM property `class`. Did you mean `className`?
ID: react/invalid-dom-prop-class
100%Fix Rate
90%Confidence
1Evidence
2023-01-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| React 16.0.0 | active | — | — | — |
| React 17.0.2 | active | — | — | — |
| React 18.2.0 | active | — | — | — |
Root Cause
Using the HTML attribute `class` instead of the JSX attribute `className` in a React component, which React interprets as an unknown DOM property.
generic中文
在 React 组件中使用了 HTML 属性 `class` 而不是 JSX 属性 `className`,React 将其解释为未知的 DOM 属性。
Official Documentation
https://reactjs.org/docs/dom-elements.html#classnameWorkarounds
-
100% success Replace all occurrences of `class` with `className` in JSX elements: `<div className="my-class">Content</div>`
Replace all occurrences of `class` with `className` in JSX elements: `<div className="my-class">Content</div>`
-
80% success If using a library that generates JSX (like Babel with custom pragma), ensure the pragma or plugin converts `class` to `className` automatically.
If using a library that generates JSX (like Babel with custom pragma), ensure the pragma or plugin converts `class` to `className` automatically.
中文步骤
Replace all occurrences of `class` with `className` in JSX elements: `<div className="my-class">Content</div>`
If using a library that generates JSX (like Babel with custom pragma), ensure the pragma or plugin converts `class` to `className` automatically.
Dead Ends
Common approaches that don't work:
-
10% fail
This is not a fix for the error; the error only occurs when `class` is used instead of `className`.
-
20% fail
This addresses a different issue (label's `for` attribute) but does not fix the `class` -> `className` problem.
-
30% fail
The error is specifically about the `class` property; using `data-class` or similar avoids the warning but does not apply CSS classes.