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

創(chuàng)新互聯建站主要業(yè)務有網站營銷策劃、成都網站設計、成都網站建設、微信公眾號開發(fā)、小程序設計、H5響應式網站、程序開發(fā)等業(yè)務。一次合作終身朋友,是我們奉行的宗旨;我們不僅僅把客戶當客戶,還把客戶視為我們的合作伙伴,在開展業(yè)務的過程中,公司還積累了豐富的行業(yè)經驗、營銷型網站資源和合作伙伴關系資源,并逐漸建立起規(guī)范的客戶服務和保障體系。
這個概念聽上去有點拗口,我們拆開了看它。
首先它本質上是一個prop,是用來父子組件之間傳遞數據用的
其次這個prop傳遞的值是一個函數
最后它取名render props,是因為它通常是用來render(渲染)某個元素或組件
比如官網給出的示例:
( Hello {data.target}
- )}/>
我們給 這個子組件傳遞了一個叫 render 的prop,這個prop的值是一個函數,它返回了一個 h1 元素。然后我們可以假裝實現一下這個 組件:
- class DataProvider extends React.Component {
- state = {
- data: {
- target: 'World'
- }
- }
- render() {
- return this.props.render(this.state)
- }
- }
最終我們的 DataProvider 組件渲染的結果就是
Hello World
。有同學可能會有疑問,為什么要費這么大周折?直接把 h1 元素寫在 DataProvider 組件里不也可以嗎?這里就要講到代碼的可復用性了,假如下次我們希望 DataProvider 組件渲染的結果就是 Hello World 呢?難道又去修改 DataProvider 組件嗎?有了render props,我們就可以動態(tài)地決定 DataProvider 組件內部要渲染的元素,同時這個元素還可以使用到 DataProvider 組件內部的數據。
實際項目案例
下面講一個實際的項目案例,下圖中我們有一個橫向滾動的 ScrollView 組件,這個組件本身是個很普通的
元素, 只不過樣式上加了 overflow-x: scroll 所以可以橫向滾動起來。產品同學說滾動區(qū)域的下方要有進度點指示,從而告訴用戶總共有幾個產品,已經現在滾到第幾個產品了。
明確了產品需求以后,我們就開始來實現,首先看下第一版:
- class demo extends Component {
- state = {
- activeIndicator: 0,
- list: []
- }
- onScroll = () => {
- const { list } = this.state;
- const container = findDOMNode(this.refs.container);
- ...
- const itemVisibleLengthInContainer = list.map((item, index) => {
- const node = findDOMNode(this.refs[`item-${index}`]);
- ...
- });
- this.setState({
- activeIndicator: active,
- });
- };
- render() {
- const { list, activeIndicator } = this.state;
- return (
- ref="container"
- horizontal={true}
- onScroll={this.onScroll}
- >
- {list.map((item,i) => (
- ref={`item-${i}`}
- data={item}
- />
- ))}
- )
- }
- }
ok,需求我們已經實現了。實現邏輯就是給 ScrollView 組件添加一個 onScroll 事件,每當滾動的時候,會先計算 ScrollView 容器的位置信息,和每一個 ProductItem 的位置信息,算出現在哪個 ProductItem 在 ScrollView 容器中所占比例最高,從而得出現在應該高亮的 activeIndicator 。
不過現在有個問題哦,給 ScrollView 組件增加進度指示器這個功能,更像是 ScrollView 組件應該支持的一個功能,而不是直接寫在業(yè)務代碼里。所以我們應該提供一個新組件 ScrollViewWithIndicator ,讓它去處理進度指示器的問題,從而跟業(yè)務解耦。
- class ScrollViewWithIndicator extends Component {
- state = {
- activeIndicator: 0,
- }
- onScroll = () => {
- const { list } = this.props;
- const container = findDOMNode(this.refs.container);
- ...
- const itemVisibleLengthInContainer = list.map((item, index) => {
- const node = findDOMNode(this.refs[`item-${index}`]);
- ...
- });
- this.setState({
- activeIndicator: active,
- });
- };
- render() {
- const [{ list, children, ...restProps } , { activeIndicator }] = [this.props, this.state];
- return (
- ref="container"
- {...restProps}
- onScroll={this.onScroll}
- >
- {list.map((item,i) => (
- {children(item}
- ))}
- )
- }
- }
然后我們的業(yè)務代碼就可以簡化了:
- class demo extends Component {
- state = {
- list: []
- }
- render() {
- const { list } = this.state;
- return (
- horizontal={true}
- list={list}
- >
- {child =>
} //(*) - )
- }
仔細看業(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


咨詢
建站咨詢
