作家
登录

如何写出漂亮的React组件

作者: 来源: 2017-09-01 16:06:14 阅读 我要评论

  •  
  •       ppu={this.props.ppu} 
  •  
  •       isLoggedIn={this.props.isLoggedIn} 
  •  
  •       gridView={this.props.isGridView} 
  •  
  •     /> 
  •  
  •   ); 
  •  
  •  
    1. render() { 
    2.  
    3.   <div class="search-results-list"
    4.  
    5.     {!!this.props.isSoftSort && <SoftSortBanner />} 
    6.  
    7.   </div> 
    8.  
    9.  

    这么多的Props估计看着都头疼,如不雅我们要将这些Props持续传入下一层,大年夜概就要变成下面这个样子了:

    1. render() { 
    2.  
    3.   const { 
    4.  
    5.     hidePriceFulfillmentDisplay, 
    6.  
    7.     primaryOffer, 
    8.  
    9.     productType, 
    10.  
    11.     productPageUrl, 
    12.  
    13.     inventory, 
    14.  
    15.     submapType, 
    16.  
    17.     ppu, 
    18.  
    19.     isLoggedIn, 
    20.  
    21.     gridView 
    22.  
    23.   } = this.props; 
    24.  
    25.   return ( 
    26.  
    27.     <ProductPrice 
    28.  
    29.       hidePriceFulfillmentDisplay={hidePriceFulfillmentDisplay} 
    30.  
    31.       primaryOffer={primaryOffer} 
    32.  
    33.       productType={productType} 
    34.  
    35.       productPageUrl={productPageUrl} 
    36.  
    37.       inventory={inventory} 
    38.  
    39.       submapType={submapType} 
    40.  
    41.       ppu={ppu} 
    42.  
    43.       isLoggedIn={isLoggedIn} 
    44.  
    45.       gridView={isGridView} 
    46.  
    47.     /> 
    48.  
    49.   ); 
    50.  
    51.  

    临时不推敲unKnown Props,我们可以应用解构赋值来实现这个功能:

    1. render() { 
    2.  
    3.   const props = this.props; 
    4.  
    5.   return <ProductPrice {...props} /> 
    6.  
    7.  

    Method Definition Shorthand

    最后这个办法不必定多有效,不过照样能让你的代码变得加倍漂亮。如不雅你欲望在Object中添加函数,你可以应用ES2015 Method Definition Shorthand来代替传统的ES5的表达式,譬如:


      推荐阅读

      第一次使用苹果操作系统需要掌握哪些知识?

    用 AirDrop 可以便利地在 Mac与Mac、iPhone与iPhone 甚至 Mac 与 iPhone 之间无线发送分享照片、视频、网站、地位等内容。速度还很快。应用时须要开启Wi-Fi和蓝牙。第一次应用苹不雅操作>>>详细阅读


    本文标题:如何写出漂亮的React组件

    地址:http://www.17bianji.com/lsqh/37109.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)