想知道如何写出漂亮的React组件吗?

开发 前端
在Walmart Labs的产品开发中,我们进行了大量的Code Review工作,这也保证了我有机会从很多优秀的工程师的代码中学习他们的代码风格与样式。在这篇博文里我会分享出我最欣赏的五种组件模式与代码片。不过我首先还是要谈谈为什么我们需要执着于提高代码的阅读体验。

在Walmart Labs的产品开发中,我们进行了大量的Code Review工作,这也保证了我有机会从很多优秀的工程师的代码中学习他们的代码风格与样式。在这篇博文里我会分享出我最欣赏的五种组件模式与代码片。不过我首先还是要谈谈为什么我们需要执着于提高代码的阅读体验。就好像你有很多种方式去装扮一只猫,如果你把你的爱猫装扮成了如下这样子:

[[201977]]

 

你或许可以认为萝卜青菜各有所爱,但是代码本身是应当保证其可读性,特别是在一个团队中,你的代码是注定要被其他人阅读的。电脑是不会在意这些的,不管你朝它们扔过去什么,它们都会老老实实的解释,但是你的队友们可不会这样,他们会把丑陋的代码扔回到你的脸上。而所谓的Pretty Components,应该包含如下的特性:

  • 即使没有任何注释的情况下也易于理解
  • 比乱麻般的代码有更好的性能表现
  • 更易于进行Bug追溯
  • 简洁明了,一句顶一万句

[[201978]] 

SFC:Stateless Functional Component

我觉得我们在开发中经常忽略掉的一个模式就是所谓的Stateless Functional Component,不过这是我个人***的React组件优化模式,没有之一。我喜爱这种模式不仅仅因为它们能够减少大量的模板代码,而且因为它们能够有效地提高组件的性能表现。总而言之,SFC能够让你的应用跑的更快,长的更帅。

[[201979]] 

直观来看,SFC就是指那些仅有一个渲染函数的组件,不过这简单的改变就可以避免很多的无意义的检测与内存分配。下面我们来看一个实践的例子来看下SFC的具体作用,譬如:

 如果我们用正统的React组件的写法,可以得出如下代码:

export default class RelatedSearch extends React.Component { 
 
  constructor(props) { 
 
    super(props); 
 
    this._handleClick = this._handleClick.bind(this); 
 
  } 
 
  _handleClick(suggestedUrl, event) { 
 
    event.preventDefault(); 
 
    this.props.onClick(suggestedUrl); 
 
  } 
 
  render() { 
 
    return ( 
 
      <section className="related-search-container"
 
        <h1 className="related-search-title">Related Searches:</h1> 
 
        <Layout x-small={2} small={3} medium={4} padded={true}> 
 
          {this.props.relatedQueries.map((query, index) => 
 
            <Link 
 
              className="related-search-link" 
 
              onClick={(event) => 
 
                this._handleClick(query.searchQuery, event)} 
 
              key={index}> 
 
              {query.searchText} 
 
            </Link> 
 
          )} 
 
        </Layout> 
 
      </section
 
    ); 
 
  } 
 
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.

而使用SFC模式的话,大概可以省下29%的代码:

const _handleClick(suggestedUrl, onClick, event) => { 
 
  event.preventDefault(); 
 
  onClick(suggestedUrl); 
 
}; 
 
const RelatedSearch = ({ relatedQueries, onClick }) => 
 
  <section className="related-search-container"
 
    <h1 className="related-search-title">Related Searches:</h1> 
 
    <Layout x-small={2} small={3} medium={4} padded={true}> 
 
      {relatedQueries.map((query, index) => 
 
        <Link 
 
          className="related-search-link" 
 
          onClick={(event) => 
 
            _handleClick(query.searchQuery, onClick, event)} 
 
          key={index}> 
 
          {query.searchText} 
 
        </Link> 
 
      )} 
 
    </Layout> 
 
  </section
 
export default RelatedSearch;  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

代码量的减少主要来源两个方面:

  • 没有构造函数(5行)
  • 以Arrow Function的方式替代Render语句(4行)

实际上,SFC最迷人的地方不仅仅是其代码量的减少,还有就是对于可读性的提高。SFC模式本身就是所谓纯组件的一种***实践范式,而移除了构造函数并且将_handleClick()这个点击事件回调函数提取出组件外,可以使JSX代码变得更加纯粹。另一个不错的地方就是SFC以Arrow Function的方式来定义了输入的Props变量,即以Object Destructring语法来声明组件所依赖的Props:

const RelatedSearch = ({ relatedQueries, onClick }) => 
  • 1.

这样不仅能够使组件的Props更加清晰明确,还能够避免冗余的this.props表达式,从而使代码的可读性更好。

[[201980]] 

***,我还想要强调下虽然我很推崇SFC,不过也不能滥用它。最合适使用SFC的地方就是之前你用纯组件的地方。在Walmart Labs中,我们使用Redux来管理应用的状态,也就意味着我们绝大部分的组件都是纯组件,也就给了SFC广阔的应用空间。一般来说,有以下特征的组件式绝对不适合使用SFC的:

  • 需要自定义整个组件的生命周期管理
  • 需要使用到refs

Conditional Components

JSX本身不支持if表达式,不过我们可以使用逻辑表达式的方式来避免将代码切分到不同的子模块中,大概是如下样子:

render() { 
 
  <div class="search-results-container"
 
    {this.props.isGrid 
 
      ? <SearchResultsGrid /> 
 
      : <SearchResultsList />} 
 
  </div> 
 
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

这种表达式在二选一渲染的时候很有效果,不过对于选择性渲染一个的情况很不友好,譬如如下的情况:

render() { 
 
  <div class="search-results-list"
 
    {this.props.isSoftSort 
 
      ? <SoftSortBanner /> 
 
      : null 
 
    } 
 
  </div> 
 
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

这样子确实能起作用,不过看上去感觉怪怪的。我们可以选用另一种更加语义化与友好的方式来实现这个功能,即使用逻辑与表达式然后返回组件:

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

不过这一点也是见仁见智,每个人按照自己的喜好来就行了。

Arrow Syntax In React And Redux

ES2015里包含了不少可口的语法糖,我***的就是那个Arrow Notation。这个特性在编写组件时很有作用:

const SoftSort = ({ hardSortUrl, sortByName, onClick }) => { 
 
  return ( 
 
    <div className="SearchInfoMessage"
 
      Showing results sorted by both Relevance and {sortByName}. 
 
      <Link 
 
        href={`?${hardSortUrl}`} 
 
        onClick={(ev) => onClick(ev, hardSortUrl)}> 
 
        Sort results by {sortByName} only 
 
      </Link> 
 
    </div> 
 
  ); 
 
};  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

该函数的功能就是返回JSX对象,我们也可以忽略return语句:

const SoftSort = ({ hardSortUrl, sortByName, onClick }) => 
 
  <div className="SearchInfoMessage"
 
    Showing results sorted by both Relevance and {sortByName}. 
 
    <Link 
 
      href={`?${hardSortUrl}`} 
 
      onClick={(ev) => onClick(ev, hardSortUrl)}> 
 
      Sort results by {sortByName} only 
 
    </Link> 
 
  </div>  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

代码行数又少了不少咯!

[[201981]] 

另一块我觉得非常适用Arrow Function的地方就是Redux的mapStateToProps函数:

const mapStateToProps = ({isLoading}) => { 
 
  return ({ 
 
    loading: isLoading, 
 
  }); 
 
};  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

需要注意的是,如果你返回的是Object,你需要包裹在大括号内:

const mapStateToProps = ({isLoading}) => ({ 
 
  loading: isLoading 
 
});  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

使用Arrow Function优化的核心点在于其能够通过专注于函数的重要部分而提升代码的整体可读性,并且避免过多的模板代码带来的噪音。

合理使用Object Destructing与Spread Attributes

大的组件往往受困于this.props过长的窘境,典型的如下所示:

render() { 
 
  return ( 
 
    <ProductPrice 
 
      hidePriceFulfillmentDisplay= 
 
       {this.props.hidePriceFulfillmentDisplay} 
 
      primaryOffer={this.props.primaryOffer} 
 
      productType={this.props.productType} 
 
      productPageUrl={this.props.productPageUrl} 
 
      inventory={this.props.inventory} 
 
      submapType={this.props.submapType} 
 
      ppu={this.props.ppu} 
 
      isLoggedIn={this.props.isLoggedIn} 
 
      gridView={this.props.isGridView} 
 
    /> 
 
  ); 
 
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

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

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

暂时不考虑unKnown Props,我们可以使用解构赋值来实现这个功能:

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

Method Definition Shorthand

***这个方法不一定多有用,不过还是能让你的代码变得更加漂亮。如果你希望在Object中添加函数,你可以使用ES2015 Method Definition Shorthand来代替传统的ES5的表达式,譬如:

如果你想设置一个默认的空方法,也可以利用这种方式:

ProductRating.defaultProps = { 
 
  onStarsClick() {} 
 
};  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
责任编辑:庞桂玉 来源: 前端大全
相关推荐

2016-11-25 13:50:15

React组件SFC

2021-12-13 14:37:37

React组件前端

2021-12-07 08:16:34

React 前端 组件

2022-08-09 13:22:26

Hooksreactvue

2022-10-14 08:45:54

2011-05-26 09:39:53

程序

2020-07-15 08:17:16

代码

2013-06-07 14:00:23

代码维护

2021-09-01 08:55:20

JavaScript代码开发

2020-05-11 15:23:58

CQRS代码命令

2017-03-15 13:41:16

数据库SQL调试

2021-11-30 10:20:24

JavaScript代码前端

2015-05-11 10:48:28

代码干净的代码越少越干净

2022-02-08 19:33:13

技巧代码格式

2022-02-17 10:05:21

CSS代码前端

2021-01-04 07:57:07

C++工具代码

2020-12-19 10:45:08

Python代码开发

2019-09-20 15:47:24

代码JavaScript副作用

2020-05-19 15:00:26

Bug代码语言

2022-03-11 12:14:43

CSS代码前端
点赞
收藏

51CTO技术栈公众号