日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯網營銷解決方案
React高階用法之RenderProps

 什么是Render Props
“render prop”是指一種在React組件之間使用值為函數的prop共享代碼的技術

創(chuàng)新互聯建站主要業(yè)務有網站營銷策劃、成都網站設計、成都網站建設、微信公眾號開發(fā)、小程序設計、H5響應式網站、程序開發(fā)等業(yè)務。一次合作終身朋友,是我們奉行的宗旨;我們不僅僅把客戶當客戶,還把客戶視為我們的合作伙伴,在開展業(yè)務的過程中,公司還積累了豐富的行業(yè)經驗、營銷型網站資源和合作伙伴關系資源,并逐漸建立起規(guī)范的客戶服務和保障體系。 

這個概念聽上去有點拗口,我們拆開了看它。

首先它本質上是一個prop,是用來父子組件之間傳遞數據用的
其次這個prop傳遞的值是一個函數
最后它取名render props,是因為它通常是用來render(渲染)某個元素或組件
比如官網給出的示例:

 
 
  1.  (
  2.   

    Hello {data.target}

  3. )}/>

我們給 這個子組件傳遞了一個叫 render 的prop,這個prop的值是一個函數,它返回了一個 h1 元素。然后我們可以假裝實現一下這個 組件:

 
 
  1. class DataProvider extends React.Component {
  2.     state = {
  3.         data: {
  4.             target: 'World'
  5.         }
  6.     }
  7.     render() {
  8.         return this.props.render(this.state)
  9.     }
  10. }

最終我們的 DataProvider 組件渲染的結果就是

Hello World

。有同學可能會有疑問,為什么要費這么大周折?直接把 h1 元素寫在 DataProvider 組件里不也可以嗎?

這里就要講到代碼的可復用性了,假如下次我們希望 DataProvider 組件渲染的結果就是 Hello World 呢?難道又去修改 DataProvider 組件嗎?有了render props,我們就可以動態(tài)地決定 DataProvider 組件內部要渲染的元素,同時這個元素還可以使用到 DataProvider 組件內部的數據。

實際項目案例
下面講一個實際的項目案例,下圖中我們有一個橫向滾動的 ScrollView 組件,這個組件本身是個很普通的

元素, 只不過樣式上加了 overflow-x: scroll 所以可以橫向滾動起來。產品同學說滾動區(qū)域的下方要有進度點指示,從而告訴用戶總共有幾個產品,已經現在滾到第幾個產品了。

明確了產品需求以后,我們就開始來實現,首先看下第一版:

 
 
  1. class demo extends Component {
  2.     state = {
  3.       activeIndicator: 0,
  4.       list: []
  5.     }
  6.     
  7.     onScroll = () => {
  8.         const { list } = this.state;
  9.         const container = findDOMNode(this.refs.container);
  10.         ...
  11.         const itemVisibleLengthInContainer = list.map((item, index) => {
  12.           const node = findDOMNode(this.refs[`item-${index}`]);
  13.            ...
  14.         });
  15.         this.setState({
  16.           activeIndicator: active,
  17.         });
  18.     };
  19.     
  20.     render() {
  21.         const { list, activeIndicator } = this.state;
  22.         return (
  23.              
  24.                 ref="container"
  25.                 horizontal={true}
  26.                 onScroll={this.onScroll}
  27.              >
  28.                 {list.map((item,i) => (
  29.                     
  30.                         ref={`item-${i}`}
  31.                         data={item}
  32.                     />
  33.                  ))}
  34.                  
  35.              
  36.              
  37.         )
  38.     }
  39. }

ok,需求我們已經實現了。實現邏輯就是給 ScrollView 組件添加一個 onScroll 事件,每當滾動的時候,會先計算 ScrollView 容器的位置信息,和每一個 ProductItem 的位置信息,算出現在哪個 ProductItem 在 ScrollView 容器中所占比例最高,從而得出現在應該高亮的 activeIndicator 。

不過現在有個問題哦,給 ScrollView 組件增加進度指示器這個功能,更像是 ScrollView 組件應該支持的一個功能,而不是直接寫在業(yè)務代碼里。所以我們應該提供一個新組件 ScrollViewWithIndicator ,讓它去處理進度指示器的問題,從而跟業(yè)務解耦。

 
 
  1. class ScrollViewWithIndicator extends Component {
  2.     state = {
  3.       activeIndicator: 0,
  4.     }
  5.     
  6.     onScroll = () => {
  7.         const { list } = this.props;
  8.         const container = findDOMNode(this.refs.container);
  9.         ...
  10.         const itemVisibleLengthInContainer = list.map((item, index) => {
  11.           const node = findDOMNode(this.refs[`item-${index}`]);
  12.            ...
  13.         });
  14.         this.setState({
  15.           activeIndicator: active,
  16.         });
  17.     };
  18.     
  19.     render() {
  20.         const [{ list, children, ...restProps } , { activeIndicator }] = [this.props, this.state];
  21.         return (
  22.              
  23.                 ref="container"
  24.                 {...restProps}
  25.                 onScroll={this.onScroll}
  26.              >
  27.                 {list.map((item,i) => (
  28.                        
  29.                         {children(item}
  30.                     
  •                  ))}
  •                  
  •              
  •              
  •         )
  •     }
  • }
  • 然后我們的業(yè)務代碼就可以簡化了:

     
     
    1. class demo extends Component {
    2.     state = {
    3.       list: []
    4.     }
    5.     render() {
    6.         const { list } = this.state;
    7.         return (
    8.               
    9.                 horizontal={true}
    10.                 list={list}
    11.              >
    12.               {child => }  //(*)
    13.              
    14.         )
    15.     }

    仔細看業(yè)務代碼demo組件,我們一共給ScrollViewWithIndicator組件傳遞了多少個props?答案是三個!分別是horizontal, list ,children,大家千萬別忘了this.props.children也是一個props哦

    再仔細看第(*)這句話,我們給ScrollViewWithIndicator組件傳遞一個叫children的prop,同時這個prop是一個函數,返回了一個組件(元素),這就是我們所說的render props啊

    為什么list.map這個數組的遍歷要寫在ScrollViewWithIndicator組件內部,而不是業(yè)務組件demo里呢?因為我們在onScroll 事件回調函數里要計算每一個商品item的位置,也就是要拿到商品item的ref屬性,所以把數組的遍歷寫在ScrollViewWithIndicator 組件內部方便我們顯性給每一個商品item聲明ref屬性


    本文題目:React高階用法之RenderProps
    本文來源:http://m.5511xx.com/article/djjoede.html