no-string-style-prop
Disallows the use of string style prop in JSX. Use an object instead.
Full Name in eslint-plugin-react-dom
react-dom/no-string-style-propFull Name in @eslint-react/eslint-plugin
@eslint-react/dom-no-string-style-propPresets
dom
recommended
strict
Rule Details
Using a string value for the style prop is not valid in React. The style prop must be an object with camelCased CSS properties. This rule helps catch incorrect string usage that would cause runtime errors.
Examples
Setting inline styles on JSX elements
function MyComponent() {
// Problem: Passing a CSS string to the style prop causes a runtime error
return <div style="color: red;" />;
}function MyComponent() {
// Recommended: Pass an object with camelCased CSS properties
return <div style={{ color: "red" }} />;
}Setting multiple inline styles
When you need to set multiple CSS properties, always pass them as a single style object with camelCased property names.
// Recommended: use a style object with camelCased properties
function Box() {
return (
<div
style={{
margin: 20,
height: 100,
width: 100,
border: "2px solid black",
backgroundColor: "blue",
}}
/>
);
}Using dynamic styles driven by state
When an element's appearance depends on state or user input, compute the style object during rendering instead of using a string.
// Recommended: compute style objects dynamically from state
import { useState } from "react";
function Pointer() {
const [position, setPosition] = useState({ x: 0, y: 0 });
return (
<div
onPointerMove={(e) => setPosition({ x: e.clientX, y: e.clientY })}
style={{
position: "absolute",
backgroundColor: "pink",
borderRadius: "50%",
opacity: 0.6,
transform: `translate(${position.x}px, ${position.y}px)`,
pointerEvents: "none",
left: -20,
top: -20,
width: 40,
height: 40,
}}
/>
);
}Versions
Resources
See Also
react-dom/no-unknown-property
Disallows unknownDOMproperties.react-jsx/no-namespace
Enforces the absence of anamespacein React elements.