Javascript react js组件中返回错误或三元运算符 let image=; 如果(isSpinnerOpened){ 图像=(
正在加载图像文件 ); } 返回( {错误?哦!没有找到文件:( {image} )} ); } 我正试图重构这段代码。我不想用let。如果出现错误,是否有机会首先返回排版,如果没有,则返回背景,如果isSpinnerOpened=true,如果isSpinnerOpened=false,则返回图像。比如:Javascript react js组件中返回错误或三元运算符 let image=; 如果(isSpinnerOpened){ 图像=(,javascript,reactjs,react-native,Javascript,Reactjs,React Native,正在加载图像文件 ); } 返回( {错误?哦!没有找到文件:( {image} )} ); } 我正试图重构这段代码。我不想用let。如果出现错误,是否有机会首先返回排版,如果没有,则返回背景,如果isSpinnerOpened=true,如果isSpinnerOpened=false,则返回图像。比如: let image = <GlobalData response={imageSize} />; if (isSpinnerOpened) { image =
let image = <GlobalData response={imageSize} />;
if (isSpinnerOpened) {
image = (
<Backdrop className={classes.backdrop} open={isSpinnerOpened}>
<CircularProgress />
<p>Loading Image File ...</p>
</Backdrop>
);
}
return (
<>
{error ? <Typography >Uh oh! No file found </Typography> : (
<>
{image}
</>
)}
</>
);
}
const image=(
正在加载图像文件
);
返回(
如果(错误){
哦!找不到文件
}否则{
isSpinnerOpened:{image}?
)
在不嵌套条件运算符的情况下(这会使内容很难读取),使用if
/else
和在不同的行上返回
s:
const image = (
<Backdrop className={classes.backdrop} open={isSpinnerOpened}>
<CircularProgress />
<p>Loading Image File ...</p>
</Backdrop>
);
return (
if (error) {
<Typography >Uh oh! No file found </Typography>
} else {
isSpinnerOpened : {image} ? <GlobalData response={imageSize} />
)
if(错误){
返回啊哦!没有找到文件;
}
返回!已打开
无效的
: (
正在加载图像文件
);
这里可以只使用条件运算符,但我认为这不是一个好主意:
if (error) {
return <Typography >Uh oh! No file found </Typography>;
}
return !isSpinnerOpened
? null
: (
<Backdrop className={classes.backdrop} open={isSpinnerOpened}>
<CircularProgress />
<p>Loading Image File ...</p>
</Backdrop>
);
返回错误
哦!没有找到文件
:!isSpinnerOpened
无效的
: (
正在加载图像文件
);
return error
? <Typography >Uh oh! No file found </Typography>
: !isSpinnerOpened
? null
: (
<Backdrop className={classes.backdrop} open={isSpinnerOpened}>
<CircularProgress />
<p>Loading Image File ...</p>
</Backdrop>
);