我正在尝试在以下示例代码中的 const def = (props) => {
行修复此 lint 错误。
const propTypes = {
prop1: PropTypes.string,
prop2: PropTypes.string,
prop3: PropTypes.string,
prop4: PropTypes.string,
prop5: PropTypes.string,
}
const abc = (props) => {
some code here }
const def = (props) => {
<div>
<div className=" ..some classes..">{abc}</div>
<div className=" ..some classes..">{t('translation/something')}</div>
<div ...>
<someComponent
do something
/>
if (some condition) {
do this
} else {
do that
}
</div>
};
知道为什么我会收到此 lint 错误吗?
原文由 User7354632781 发布,翻译遵循 CC BY-SA 4.0 许可协议
您不会返回任何内容,至少从您的代码段和评论中是这样。
const def = (props) => { <div></div> };
这没有返回任何东西,你用大括号包裹了箭头函数的主体但没有返回值。
const def = (props) => { return (<div></div>); };
或const def = (props) => <div></div>;
另一方面,这两个解决方案返回一个有效的 React 组件。还要记住,在你的
jsx
中(如@Adam 所提到的)你不能有if ... else ...
但只有三元运算符。