反应:期望一个赋值或函数调用,而是看到一个表达式 [英] React: Expected an assignment or function call and instead saw an expression
问题描述
我正在尝试在下面的示例代码中的const def = (props) => {
行修复此皮棉错误.
I am trying to fix this lint error at line const def = (props) => {
in following sample code.
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>
};
有人知道为什么我会出现此皮棉错误吗?
Any idea why i am getting this lint error?
推荐答案
您至少从代码段和评论中都不会返回任何内容.
You are not returning anything, at least from your snippet and comment.
const def = (props) => { <div></div> };
这不返回任何内容,您用花括号将arrow函数的主体包裹起来,但是没有返回值.
This is not returning anything, you are wrapping the body of the arrow function with curly braces but there is no return value.
const def = (props) => { return (<div></div>); };
或
const def = (props) => <div></div>;
另一方面,这两个解决方案正在返回有效的React组件.还要记住,在jsx
内(如@Adam所述),您不能具有if ... else ...
,而只能具有三元运算符.
These two solutions on the other hand are returning a valid React component. Keep also in mind that inside your jsx
(as mentioned by @Adam) you can't have if ... else ...
but only ternary operators.
这篇关于反应:期望一个赋值或函数调用,而是看到一个表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!