2017-07-27 74 views
0

我正在从mongodb格式化reactjs中的日期。我使用下面的代码在UI中显示它。ReactJS - 瞬间 - 反应时刻

<td> <Moment format="DD-MMM-YYYY">{ this.props.item.date }</Moment></td> 

如果日期不可用,则打印当前日期。如果日期不存在于mongodb中,如何打印null?

回答

0

您可以使用表达式来处理例如

<td> 
    <Moment format="DD-MMM-YYYY"> 
    { this.props.item.date ? this.props.item.date : null } 
    </Moment> 
</td> 

如果你的意思是不显示任何内容,然后把元素像这样

<td> 
    { this.props.item.date ? 
    <Moment format="DD-MMM-YYYY"> 
    {this.props.item.date} 
    </Moment> : null 
    } 
</td> 
+0

谢谢。它显示“Invalid Date”而不是null或“” – Karthikeyan

+0

@Karthikeyan使用我发布的第二个代码。 –

+1

非常感谢..它的工作.... – Karthikeyan

0

上述表达这是你在找什么?

<td> <Moment format="DD-MMM-YYYY">{ this.props.item.date ? this.props.item.date : 'null' }</Moment></td>

可以使用三元表达式有条件地呈现组件。