React property name in React DOM element should be pre-defined one
- REACT_BAD_UNKNOWN_PROP
- Error
- Medium
- react
This rule applies when a wrong React property name is used for React DOM element.
In React, DOM attributes and properties are supported as camelCased names, and data-* and aria-* attributes should be lowercased. Therefore, it is possible to confuse DOM attribute name with React property name, and React will output a warning message.
It can be applied to the following:
- DOM attribute name is used instead of React DOM property (e.g.
classinstead ofclassName). - DOM event handler property is used instead of React event handler (e.g.
onclickinstead ofonClick). data-*andaria-*attributes are not used as lowercased names (e.g.data-Xinstead ofdata-x).
Noncompliant Code Example
View with compliant examples side by sideimport { createRoot } from 'react-dom/client';
const root = createRoot(document.getElementById('root'));
root.render(
<>
<div class="hello"> {/* REACT_BAD_UNKNOWN_PROP alarm because 'className' should be used instead of 'class' */}
Example 1
</div>
<div onclick={() => alert('clicked')}> {/* REACT_BAD_UNKNOWN_PROP alarm because 'onClick' should be used instead of 'onclick' */}
Example 2
</div>
<div data-X="foo"> {/* REACT_BAD_UNKNOWN_PROP alarm because 'data-x' should be used instead of 'data-X'. */}
Example 3
</div>
</>
);Compliant Code Example
View with noncompliant examples side by sideimport { createRoot } from 'react-dom/client';
const root = createRoot(document.getElementById('root'));
root.render(
<>
<div className="hello">
Example 1
</div>
<div onClick={() => alert('clicked')}>
Example 2
</div>
<div data-x="foo">
Example 3
</div>
</>
);Version
This rule was introduced in DeepScan 1.2.0-alpha.
See
React Warning: Invalid DOM property
class. Did you meanclassName?React Warning: Invalid event handler property
onclick. Did you meanonClick?React Warning: React does not recognize the
data-Xprop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercasedata-xinstead. If you accidentally passed it from a parent component, remove it from the DOM element.