JS.REACT.STYLE.PROP.OBJECT

Enforce style prop value is an object

Require that the value of the prop style be an object or a variable that is an object.

Rule Details

Examples of incorrect code for this rule:

Copy
<div style="color: 'red'" />

<div style={true} />

<Hello style={true} />

const styles = true;
<div style={styles} />
Copy
React.createElement("div", { style: "color: 'red'" });

React.createElement("div", { style: true });

React.createElement("Hello", { style: true });

const styles = true;
React.createElement("div", { style: styles });

Examples of correct code for this rule:

Copy
<div style={{ color: "red" }} />

<Hello style={{ color: "red" }} />

const styles = { color: "red" };
<div style={styles} />
Copy
React.createElement("div", { style: { color: 'red' }});

React.createElement("Hello", { style: { color: 'red' }});

const styles = { height: '100px' };
React.createElement("div", { style: styles });

Rule Options

Copy
...
"react/style-prop-object": [<enabled>, {
  "allow": [<string>]
}]
...

allow

A list of elements that are allowed to have a non-object value in their style attribute. The default value is [].

Example

Copy
{
  "allow": ["MyComponent"]
}

Examples of incorrect code for this rule:

Copy
<Hello style="a string">
React.createElement(Hello, { style: "some styling" });

Examples of correct code for this rule:

Copy
<MyComponent style="a string">
React.createElement(MyComponent, { style: "some styling" });

The content on this page is adapted from the ESLint User Guide. Copyright © OpenJS Foundation and other contributors, www.openjsf.org. All rights reserved. https://eslint.org/docs/rules/