Karthikeyan Karthikeyan - 1 year ago 238
React JSX Question

ReactJS - Moment- react-moment

Am formatting date from mongodb in reactjs. Am using the below code to display in UI.

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

but, if the date is not available then, by default its printing current date. How to print null if the date is not presented in mongodb.

Please suggest.

Answer Source

You can use expressions to handle that e.g.

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

If you meant to display nothing then put the expression above the element something like this

   { this.props.item.date ?
   <Moment format="DD-MMM-YYYY">
   </Moment> : null
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download