为了账号安全,请及时绑定邮箱和手机立即绑定

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

{
            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 回答

重复的代码写成一个组件

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信