Configurations
ESLint React provides the following configurations:
Settings
version
const version = "detect"
React version to perform the analysis, "detect"
means auto detect React version from the project’s dependencies.
If failed to detect, it will use the 19.0.0
version.
importSource
const importSource = "react"
importSource
is specified, an equivalent version of React should be provided to the version
setting.The source where React is imported from.
This allows to specify a custom import location for React when not using the official distribution.
For example, if you are using @pika/react
instead of react
, you can set the importSource
to @pika/react
:
import React from "@pika/react";
polymorphicPropName
const polymorphicPropName = "as"
You can optionally use the polymorphicPropName
setting to define the prop your code uses to create polymorphic components. This setting will be used determine the element type in rules that require semantic context.
For example, if you set the polymorphicPropName
setting to as
then this element:
<Box as="h3">Configurations</Box>
will be evaluated as an h3
.
If no polymorphicPropName
is set, then the component will be evaluated as Box
.
additionalComponents
const additionalComponents = [] satisfies {
name: string;
as?: string;
attributes?: {
name: string;
as?: string;
defaultValue?: string;
}[]
}[]
additionalComponents
, consider whether polymorphicPropName
can be used instead, as it simpler and more efficient.An array of components and its attributes mapping. It allows the related rules to do even more comprehensive analysis. You can also provide default values for attributes here, that will be used when that attribute is not present.
For example, if you set the additionalComponents
to:
[
{
"name": "EmbedContent",
"as": "iframe",
"attributes": [
{
"name": "sandbox",
"defaultValue": ""
}
]
}
]
then this element:
<EmbedContent src="https://eslint-react.xyz" />
will be evaluated as an:
<iframe src="https://eslint-react.xyz" sandbox="" />
So that both the dom/no-missing-iframe-sandbox
and dom/no-unsafe-iframe-sandbox
rules can perform checks on it.
additionalHooks
const additionalHooks = {
useLayoutEffect: ["useIsomorphicLayoutEffect"]
// ...
}
A object of aliases for React built-in Hooks. ESLint React will recognize these aliases as equivalent to the built-in Hooks in all its rules.
For example, if you set the additionalHooks
to:
{
useLayoutEffect: ["useIsomorphicLayoutEffect"]
}
then the React Hook call:
useIsomorphicLayoutEffect(() => { setCount(count => count + 1); }, []);
will be evaluated as an:
useLayoutEffect(() => { setCount(count => count + 1); }, []);
So that the hooks-extra/no-direct-set-state-in-use-layout-effect
rule can perform checks on it.
Examples
import eslintReact from "@eslint-react/eslint-plugin";
export default [
// ...
{
files: ["**/*.{ts,tsx}"],
...eslintReact.configs.recommended,
settings: {
"react-x": {
version: "19.0.0",
additionalHooks: {
useLayoutEffect: ["useIsomorphicLayoutEffect"],
},
additionalComponents: [
{
name: "Link",
as: "a",
attributes: [
{ name: "to", as: "href" },
],
},
{
name: "EmbedContent",
as: "iframe",
attributes: [
{ name: "sandbox", defaultValue: "" }
],
},
],
},
},
},
];