Reputation: 5434
Is there a rule where in I can disable usage of React.StatelessComponent
or React.FunctionalComponent
to use only React.FC
For example:
export const ComponentOne: React.StatelessComponent<Props> = (props) => { return <....> };
export const ComponentTwo: React.FunctionalComponent<Props> = (props) => { return <....> };
Should be enforced by ESLint to be written as
export const ComponentOne: React.FC<Props> = (props) => { return <....> };
export const ComponentTwo: React.FC<Props> = (props) => { return <....> };
I think it's possible via no-restricted-syntax
rule but can't figure out from the documentation.
Upvotes: 2
Views: 2116
Reputation: 12323
How to ban React.FC
(why? https://github.com/facebook/create-react-app/pull/8177):
"@typescript-eslint/no-restricted-types": [
"error",
{
types: {
"React.FC": {
message: "Useless and has some drawbacks, see https://github.com/facebook/create-react-app/pull/8177",
},
"React.FunctionComponent": {
message: "Useless and has some drawbacks, see https://github.com/facebook/create-react-app/pull/8177",
},
"React.FunctionalComponent": {
message:
"Preact specific, useless and has some drawbacks, see https://github.com/facebook/create-react-app/pull/8177",
},
},
},
],
React.StatelessComponent
and React.SFC
have been removed from @types/react
>= 18
React.VoidFunctionComponent
and React.VFC
are deprecated and will eventually be removed (thus no need to ban them via ESLint)
Upvotes: 6
Reputation: 5434
Ok was finally able to figure it out, can achieve this using the rule @typescript-eslint/ban-types
"@typescript-eslint/ban-types": ["error",
{
"types": {
"React.StatelessComponent": { "message": "Please use React.FC instead.", "fixWith": "React.FC" },
"React.FunctionalComponent": { "message": "Please use React.FC instead.", "fixWith": "React.FC" },
}
}
]
Upvotes: 4