I have been struggling all morning with this issue and couldn't find the solution anywhere. I am new to typescript, and I am trying to set it up properly with Eslint and Prettier to ensure the code is properly formated.
So, the issue I am facing when creating functional components. As per the cheatsheet, I am trying to export a simple component such as:
import React from "react";
type DivProps = {
text: string;
};
const Div = ({ text }: DivProps): JSX.Element => (<div>{text}</div>)
export default Div;
However, eslint is complaining, saying that "Function component is not a function expression".
When running fix, it will convert my function to an unnamed function:
const Div = function ({ text }: DivProps): JSX.Element {
return <div>{text}</div>;
};
And then it will complain saying "Unexpected unnamed function.".
Even if I try to refactor the function to:
function Div({ text }: DivProps): JSX.Element {
return <div>{text}</div>;
};
I still get the same error saying that "Function component is not a function expression".
My .eslintrc.js file is:
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: ["airbnb", "plugin:@typescript-eslint/recommended", "prettier"],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 12,
sourceType: "module",
},
plugins: ["@typescript-eslint", "react-hooks", "prettier"],
rules: {
"react-hooks/rules-of-hooks": "error",
"prettier/prettier": "error",
"no-use-before-define": "off",
"@typescript-eslint/no-use-before-define": ["error"],
"react/jsx-filename-extension": [
1,
{
extensions: [".tsx"],
},
],
"import/prefer-default-export": "off",
"import/extensions": [
"error",
"ignorePackages",
{
ts: "never",
tsx: "never",
},
],
},
settings: {
"import/resolver": {
typescript: {},
},
},
};
Appreciate any help on this!
PS: If you see any other thing you would amend/improve in the eslintrc file let me know. As I mentioned, I am new to TypeScript so I am still trying to find out the best options for linting.
Cheers!
Alejandro