我有一个组件如下。
import React, { SetStateAction } from 'react';
interface ColorObject {
name: string,
slug: string,
hex: string,
}
interface Props {
color: ColorObject
onClick: React.Dispatch<SetStateAction<ColorObject>>,
}
const Color: React.FC<Props> = (props) => {
const { color, onClick } = props;
const selectColor = () => {
onClick(color);
};
return (
<button type="button" className="color" onClick={selectColor}>
<div className="block" style={{ backgroundColor: color.hex }} />
<p className="color-label">{color.name}</p>
</button>
);
};
export default React.memo(Color);
一切正常,但SetStateAction
导入语句中的红色灯显示以下错误。
'SetStateAction' 已定义但从未使用过。eslint(没有未使用的变量)
但是,正如您所看到的,我在 Props 界面中使用它。在不替换上述界面中的SetStateAction
with的情况下,我该如何克服这个问题。React.SetStateAction
下面是我的 .eslintrc.json 文件。
{
"env": {
"browser": true,
"es6": true
},
"extends": [
"plugin:react/recommended",
"airbnb"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"react",
"@typescript-eslint"
],
"rules": {
"react/jsx-filename-extension": [
1,
{
"extensions": [
".tsx",
".ts"
]
}
],
"import/extensions": [
"error",
"ignorePackages",
{
"js": "never",
"mjs": "never",
"jsx": "never",
"ts": "never",
"tsx": "never"
}
],
"react/prop-types": 0
},
"settings": {
"import/resolver": {
"node": {
"extensions": [
".js",
".jsx",
".ts",
".tsx"
]
}
}
}
}
我使用了 eslint --init 并选择了 airbnb 风格指南。在选择中也选择打字稿。