if 语句不作为对象返回 React JS
Posted
技术标签:
【中文标题】if 语句不作为对象返回 React JS【英文标题】:if statement not returning as an object React JS 【发布时间】:2021-04-14 04:36:05 【问题描述】:我正在尝试在样式标签中编写两个表达式,但它一直给我以下错误The `style` prop expects a mapping from style properties to values, not a string. For example, style=marginRight: spacing + 'em' when using JSX.
我该如何解决?
<table id='calendario'>
<tbody>
linhas.map((linha) => (
<tr key=linha>
colunas.map((coluna , e , i) => (
<td
key=coluna onLoad=() =>
if(e < index.length)e = e;
if(i >= proximo_mes.length)i = i;
style=() => if(linha == 0 && coluna == e)return filter : 'brightness(0.6)';
if(linha == 4 && coluna == i)returnfilter : 'brightness(0.6)'
elsereturnfilter : 'brightness(1)'
></td>
))
</tr>
))
</tbody>
</table>
【问题讨论】:
【参考方案1】:像这样使用ternary operator:
style=(linha == 0 && coluna == e) ? filter : 'brightness(0.6)' : (linha == 4 && coluna == i) ? filter : 'brightness(0.6)' : filter : 'brightness(1)'
或者这个:
style=filter :(linha == 0 && coluna == e) ? 'brightness(0.6)' : (linha == 4 && coluna == i) ?'brightness(0.6)' : 'brightness(1)'
发生错误是因为您返回的是function
而不是object
,请查看以下示例:
console.log("Function Style: ",
style: () =>
if (true)
return filter: "brightness(0.6)" ;
)
console.log("Ternary Style: ",
style: true ? filter: "brightness(0.6)" : filter: "brightness(1)"
)
【讨论】:
非常感谢,但你能告诉我为什么我的没用吗? 没问题,再次检查答案我为这种情况做一个例子。以上是关于if 语句不作为对象返回 React JS的主要内容,如果未能解决你的问题,请参考以下文章
如何仅使用 React Native 中的 if 语句返回 useEffect
错误:应用程序(...):渲染没有返回任何内容。这通常意味着缺少返回语句 discord.js react typescript