简体   繁体   中英

ReactJS - Moment- react-moment

I'm formatting date from mongodb in reactjs. I'm using the below code to display it in UI.

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

If the date is not available then it's printing the current date. How to print null if the date is not present in mongodb?

You can use expressions to handle that eg

<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>

Is this what you are looking for?

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

You can use a ternary expression to conditionally render components.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM