条件判断

  • if else
  • 三元表达式
  • 逻辑运算符 && ||

    1. render() {
    2. const blackBtn = <button className="btn">blackBtn</button>
    3. const whiteBtn = <button className="btn">whiteBtn</button>
    4. if (this.state.theme === 'black') {
    5. return blackBtn;
    6. } else {
    7. return whiteBtn;
    8. }
    9. }
    return <div>{ this.state.theme === 'black' ? blackBtn : whiteBtn }</div>
    
    return <div>{ this.state.theme === 'black' && blackBtn }</div>
    

    列表渲染

  • map

  • key ```jsx this.state = { list: [ {
    id: 'id-1',
    title: 'title1'
    
    }, {
    id: 'id-2',
    title: 'title2'
    
    }, {
    id: 'id-3',
    title: 'title3'
    
    } ] }

// 这里的key和vue的key一样,必填 render() { return

    { this.state.list.map((item, index) => { return
  • {item.title}

  • })}
} ```