Tuesday, May 26, 2020

[React][Example] Create ul list element with for-loop

You need to create an empty array and use push add items to it.
class Date extends React.Component {
  getMonthList(){
    let date = new Date();
    let startYear = 2000
    let currentYear = date.getFullYear()
    let items = []
    for(let i=currentYear; i>=startYear; i--){
      items.push(<li>{i}</li>)
    }
    return <ul>{items}</ul>
  }
  render() {
    return (
      <div>{this.getMonthList()}</div>
  )}
}


Reference
https://ithelp.ithome.com.tw/articles/10201227

No comments :

Post a Comment