优化问题,map列表时候,我加了个图片是否为空的判断,代码没错,但写的冗余,如何优化?

来源:1-1 react的基本概念

Hui前端框架

2018-09-06 12:26

{
            list.map((item, index)=> {
              return (
                <li key={index} className="item" data-id={item.get('id')}>
                  {
                    item.get('imgUrl') ===''
                    ?
                    <div>
                      <div className="item-title">
                        <Link to={'/detail/' + item.get('id')}>{item.get('title')}</Link>
                      </div>
                      <div className="item-desc">
                        {item.get('desc')}
                      </div>
                    </div>
                    :
                    <div className="has-img">
                     <Link className="pic-box" to={'/detail/' + item.get('id')}><img alt="" src={item.get('imgUrl')} /></Link>
                     <div className="item-title">
                       <Link to={'/detail/' + item.get('id')}>{item.get('title')}</Link>
                     </div>
                     <div className="item-desc">
                       {item.get('desc')}
                     </div>
                    </div>
                  }
                </li>
              )
            })
          }


写回答 关注

1回答

  • 北鸟南游
    2018-11-13 11:02:31

    重复的代码写成一个组件

React组件

带你了解如何创建好拥有各自状态的组件,再由组件构成更加复杂的界面

26078 学习 · 27 问题

查看课程

相似问题