【发布时间】:2018-03-17 20:10:04
【问题描述】:
我正在尝试对齐 Button 组件中的图标。传递 iconAlign 属性,如果它是正确的,则图标将首先呈现,如果左则文本将稍后呈现,反之亦然。但是我通过流程得到了这个错误
React element `Icon`: 'object type'. See ./tmp/flow/flowlib_16523b66/react.js:159. This type cannot be coerced to 'string'.
这是我的组件
<ButtonInner>
{
iconAlign === 'right' ? `${text} ${icon && <Icon color={iconColor} />}` :
`${icon && <Icon color={iconColor} />} ${text}`
}
</ButtonInner>
我错过了什么吗?请问有更好的选择吗?
【问题讨论】:
-
您不能在字符串文字中使用
<Icon>,它会尝试将对象强制转换为字符串,但它有一个特殊的 toString() 来响应捕获和抛出。重构它以单独处理文本。iconAlign === 'right' ? ({text} <Icon />) : (<Icon /> {text})- 虽然你可以解决基于 CSS 的定位而不是调整项目的顺序。 -
是的,我明白了。谢谢@DimitarChristoff 我找到了解决方案
标签: reactjs ecmascript-6 flowtype