reactjs - 三元表达式意外令牌错误
问题描述
我在三元代码表达式中遇到了意外的令牌错误。错误消息指向 === 中的第一个 =。我在这里做错了什么?
const getMyHeader = (heading, advisorTags) => {
if (advisorTags.length > 0) {
return (
<Typography
variant="h6"
component="h6"
style={{
heading === 'White Advantages'
? 'fontSize: 16'
: 'fontSize: 16, paddingTop: 10'
}}
>
{heading}
</Typography>
);
} else {
return '';
}
};
解决方案
style prop 接受常规对象,'fontSize: 16, paddingTop: 10'
不起作用,这很可能导致错误。
你可以试试这样的
const getMyHeader = (heading, advisorTags) => {
if (advisorTags.length > 0) {
let style = { fontSize: 16 };
if(heading === 'White Advantages') {
style.paddingTop = 10;
}
return (
<Typography
variant="h6"
component="h6"
style={style}
>
{heading}
</Typography>
);
} else {
return '';
}
};
推荐阅读
- security - 如何使用 .net core 2 正确实施 AES 加密?
- javascript - 将包含文章列表的数组过滤为对象
- alert - 我在我的 ESLint 文件中做错了什么,因此不会提醒导入?
- angular - 使用令牌 JWT Angular Auth 的唯一会话
- python - 无法导入模块“lambda_function”:在 AWS 上使用 python 没有名为“twilio”的模块
- html - 第一行 3 张图像,然后在下面的第二行 2 张图像
- laravel - 如何让用户选择在 Element-ui 中可以选择多少个复选框?
- python - 在 Ipywidget 中更改 Label 内容的对齐方式
- r - 动态创建新变量,然后在 R 的循环中调用该变量
- reactjs - React Native 导航在第一次点击时返回,但第二次可以工作