我目前正在将我的应用程序更新为React 16.8,因此我可以使用很棒的新钩子功能。更新React包(以及所有依赖项)不是问题,一切正常。但是,当我尝试设置ESLint时,在尝试使用钩子时总是出现以下错误:
在函数“ projectInfo”中调用“反应挂钩”“ useEffect”,该项目既不是React函数组件也不是自定义的React Hook函数
我的组件看起来像这样:
import React, { useState} from 'react';
const myComponent = () => {
const [counter, setCounter] = useState(0);
return <span onClick={() => setCounter(counter + 1)}>{counter}</span>;
};
export default myComponent;
我的.eslintsrc
文件如下:
{
"extends": ["react-app"],
"plugins": [
"react-hooks"
],
"rules": {
"no-console": ["warn", { "allow": ["info", "error"] }],
"quotes": ["warn", "single"],
"semi": ["warn", "always"],
"no-debugger": ["warn"],
"eqeqeq": ["warn"],
"no-else-return": ["warn"],
"no-extra-bind": ["warn"],
"jsx-a11y/href-no-hash": false,
"prefer-destructuring": [
"warn",
{
"array": true,
"object": true
},
{
"enforceForRenamedProperties": false
}
],
"react-hooks/rules-of-hooks": "warn"
}
}
编辑:
错误中指出projectInfo
中存在错误。为简单起见,我使用上面的myComponent
代码对此进行了更改。 projectinfo看起来像这样:
const projectInfo = props => {
const [createdLink, setCreatedLink] = useState(null);
const [getProjectStatus, asyncGetProject] = useAsyncCall(props.getProject);
const [generateLinkStatus, asyncGenerateLink] = useAsyncCall(api.questionnaire.generateQuestionnaireToken);
// ComponentWillMount
useEffect(() => {
if (!props.project) {
asyncGetProject(props.match.params.id);
}
}, []);
const generateQuestionnaireLink = async () => {
const response = await asyncGenerateLink(props.project.questionnaireId);
const createdLink = `${window.location.host}/questionnaire/${response.data.id}`;
setCreatedLink(createdLink);
};
const { translate, project, updateProject } = props;
const errorMessage = generateLinkStatus.error.message || getProjectStatus.error.message;
return (
<div className={styles.profileContainer}>
<Message message={errorMessage} status={'error'} />
<BackButton />
<Loading isVisible={getProjectStatus.loading} />
{project && (
<Fragment>
<EditableForm entity={project} onSubmit={updateProject}>
<Label text={translate('projectName')} name={'name'} />
<Checkbox text={translate('isPublic')} name={'isPublic'} />
</EditableForm>
<Loading isVisible={generateLinkStatus.loading} />
<Button text={translate('generateButton')} clickHandler={generateQuestionnaireLink} />
<br />
{createdLink && <span>{createdLink}</span>}
</Fragment>
)}
</div>
);
};
const mapStateToProps = state => {
return {
translate: getTranslate(state.locale),
project: state.project.currentProject
};
};
const mapDispatchToProps = dispatch => {
return {
getProject: id => dispatch(actions.getProject(id)),
updateProject: (id, params) => dispatch(actions.updateProject(id, params))
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(projectInfo);
答案 0 :(得分:4)
这里的问题是您的组件名称以小写字母开头。
请参见eslint-plugin-react-hooks code source
/**
* Checks if the node is a React component name. React component names must
* always start with a non-lowercase letter. So `MyComponent` or `_MyComponent`
* are valid component names for instance.
*/
function isComponentName(node) {
if (node.type === 'Identifier') {
return !/^[a-z]/.test(node.name);
} else {
return false;
}
}