我正在尝试在JSX中呈现布尔值,但是React将其作为表达式进行评估,并且在返回组件后未返回任何内容。
任何解决方法?
这是一个例子
var ipsumText = true; ReactDOM.render( <div> Boolean Value: {ipsumText} </div>, document.getElementById('impl') );
只是将编译后的HTML显示为
<div data-reactid=".0"><span data-reactid=".0.0">Boolean Value: </span></div>
编辑: 这是示例http://jsbin.com/nibihodoce/1/edit?html,js,output的JSBin链接
编辑2: 我已经探索了.toString()替代方案,但是,因为我正在遍历对象数组,并且该对象的特定字段可以具有字符串/整数/布尔值。将.toString()应用于所有’em似乎不是最佳方法。
Boolean Value: { ipsumText.toString() }
UPD:或
Boolean Value: { String(ipsumText) }
要么
Boolean Value: { '' + ipsumText }
{`Boolean Value: ${ipsumText}`}
Boolean Value: { JSON.stringify(ipsumText) }
我更喜欢第二种选择。通用,快速,适用于所有原始类型: Boolean( smth ),Number( smth )。
Boolean( smth )
Number( smth )