Karthikeyan Karthikeyan - 1 year ago 231
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.

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

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

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