最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用react-virtualized實(shí)現(xiàn)圖片動態(tài)高度長列表的問題

 更新時間:2021年05月28日 09:20:38   作者:yuanzili  
一般我們在寫react項(xiàng)目中,同時渲染很多dom節(jié)點(diǎn),會造成頁面卡頓, 空白的情況。為了解決這個問題,今天小編給大家分享一篇教程關(guān)于react-virtualized實(shí)現(xiàn)圖片動態(tài)高度長列表的問題,感興趣的朋友跟隨小編一起看看吧

虛擬列表是一種根據(jù)滾動容器元素的可視區(qū)域來渲染長列表數(shù)據(jù)中某一個部分?jǐn)?shù)據(jù)的技術(shù)。虛擬列表是對長列表場景一種常見的優(yōu)化,畢竟很少有人在列表中渲染上百個子元素,只需要在滾動條橫向或縱向滾動時將可視區(qū)域內(nèi)的元素渲染出即可。

開發(fā)中遇到的問題

1.長列表中的圖片要保持原圖片相同的比例,那縱向滾動在寬度不變的情況下,每張圖片的高度就是動態(tài)的,當(dāng)該列表項(xiàng)高度發(fā)生了變化,會影響該列表項(xiàng)及其之后所有列表項(xiàng)的位置信息。

2.圖片width,height必須在圖片加載完成后才能獲得.

解決方案

我們使用react-virtualized中l(wèi)ist組件,官方給出的例子

import React from 'react';
import ReactDOM from 'react-dom';
import {List} from 'react-virtualized';

// List data as an array of strings
const list = [
  'Brian Vaughn',
  // And so on...
];

function rowRenderer({
  key, // Unique key within array of rows
  index, // Index of row within collection
  isScrolling, // The List is currently being scrolled
  isVisible, // This row is visible within the List (eg it is not an overscanned row)
  style, // Style object to be applied to row (to position it)
}) {
  return (
    <div key={key} style={style}>
      {list[index]}
    </div>
  );
}

// Render your list
ReactDOM.render(
  <List
    width={300}
    height={300}
    rowCount={list.length}
    rowHeight={20}
    rowRenderer={rowRenderer}
  />,
  document.getElementById('example'),
);

其中rowHeight是每一行的高度,可以傳入固定高度也可以傳入function。每次子元素高度改變需要調(diào)用recomputeRowHeights方法,指定索引后重新計算行高度和偏移量。

具體實(shí)現(xiàn)

const ImgHeightComponent = ({ imgUrl, onHeightReady, height, width }) => {
  const [style, setStyle] = useState({
    height,
    width,
    display: 'block',
  })
  const getImgWithAndHeight = (url) => {
    return new Promise((resolve, reject) => {
      var img = new Image()
      // 改變圖片的src
      img.src = url
      let set = null
      const onload = () => {
        if (img.width || img.height) {
          //圖片加載完成
          clearInterval(set)
          resolve({ width: img.width, height: img.height })
        }
      }
      set = setInterval(onload, 40)
    })
  }

  useEffect(() => {
    getImgWithAndHeight(imgUrl).then((size) => {
      const currentHeight = size.height * (width / size.width)
      setStyle({
        height: currentHeight,
        width: width,
        display: 'block',
      })
      onHeightReady(currentHeight)
    })
  }, [])
  return <img src={imgUrl} alt=''  style={style} />
}

先寫一個獲取圖片高度的組件,通過定時循環(huán)檢測獲取并計算出高度傳給父組件。

import React, { useState, useEffect, useRef } from 'react'
import styles from './index.scss'
import { AutoSizer } from 'react-virtualized/dist/commonjs/AutoSizer'
import { List  } from 'react-virtualized/dist/commonjs/List'

export default class DocumentStudy extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      list: [], 
      heights: [],
      autoWidth:900,
      autoHeight: 300
    }
  }

  handleHeightReady = (height, index) => {
    this.setState(
      (state) => {
        const flag = state.heights.some((item) => item.index === index)
        if (!flag) {
          return {
            heights: [
              ...state.heights,
              {
                index,
                height,
              },
            ],
          }
        }
        return {
          heights: state.heights,
        }
      },
      () => {
        this.listRef.recomputeRowHeights(index)
      },
    )
  }

  getRowHeight = ({ index }) => {
    const row = this.state.heights.find((item) => item.index === index)
    return row ? row.height : this.state.autoHeight
  }

  renderItem = ({ index, key, style }) => {
    const { list, autoWidth, autoHeight } = this.state
    if (this.state.heights.find((item) => item.index === index)) {
      return (
        <div key={key} style={style}>
          <img src={list[index].imgUrl}  alt='' style={{width: '100%'}}/>
        </div>
      )
    }

    return (
      <div key={key} style={style}>
        <ImgHeightComponent
          imgUrl={list[index].imgUrl}
          width={autoWidth}
          height={autoHeight}
          onHeightReady={(height) => {
            this.handleHeightReady(height, index)
          }}
        />
      </div>
    )
  }

  render() {
    const { list } = this.state
    return (
      <>
        <div style={{ height: 1000 }}>
          <AutoSizer>
            {({ width, height }) => (
              <List
                ref={(ref) => (this.listRef = ref)}
                width={width}
                height={height}
                overscanRowCount={10}
                rowCount={list.length}
                rowRenderer={this.renderItem}
                rowHeight={this.getRowHeight}
              />
            )}
          </AutoSizer>
        </div>
      </>
    )
  }
}

父組件通過handleHeightReady方法收集所有圖片的高度,并在每一次高度改變調(diào)用List組件的recomputeRowHeights方法通知組件重新計算高度和偏移。到這里基本已經(jīng)解決遇到的問題。

實(shí)際效果

小結(jié)

目前只是使用react-virtualized來完成圖片長列表實(shí)現(xiàn),具體react-virtualized內(nèi)部實(shí)現(xiàn)還需要進(jìn)一步研究。

以上就是用react-virtualized實(shí)現(xiàn)圖片動態(tài)高度長列表的詳細(xì)內(nèi)容,更多關(guān)于react virtualized長列表的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • React 中合成事件的實(shí)現(xiàn)示例

    React 中合成事件的實(shí)現(xiàn)示例

    本文主要介紹了React 中合成事件的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-02-02
  • React手稿之 React-Saga的詳解

    React手稿之 React-Saga的詳解

    這篇文章主要介紹了React手稿之 React-Saga的詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • React SSR服務(wù)端渲染的實(shí)現(xiàn)示例

    React SSR服務(wù)端渲染的實(shí)現(xiàn)示例

    本文主要介紹了實(shí)現(xiàn)React服務(wù)端渲染,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • React?SSR架構(gòu)Stream?Rendering與Suspense?for?Data?Fetching

    React?SSR架構(gòu)Stream?Rendering與Suspense?for?Data?Fetching

    這篇文章主要為大家介紹了React?SSR架構(gòu)Stream?Rendering與Suspense?for?Data?Fetching解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • react 實(shí)現(xiàn)圖片正在加載中 加載完成 加載失敗三個階段的原理解析

    react 實(shí)現(xiàn)圖片正在加載中 加載完成 加載失敗三個階段的原理解析

    這篇文章主要介紹了react 實(shí)現(xiàn)圖片正在加載中 加載完成 加載失敗三個階段的,通過使用loading的圖片來占位,具體原理解析及實(shí)現(xiàn)代碼跟隨小編一起通過本文學(xué)習(xí)吧
    2021-05-05
  • React tsx生成隨機(jī)驗(yàn)證碼

    React tsx生成隨機(jī)驗(yàn)證碼

    這篇文章主要為大家詳細(xì)介紹了React tsx生成隨機(jī)驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • 淺談React多個setState會調(diào)用幾次

    淺談React多個setState會調(diào)用幾次

    在React的生命周期鉤子和合成事件中,多次執(zhí)行setState,會被調(diào)用幾次,本文就詳細(xì)的介紹一下,感興趣的可以了解一下
    2021-11-11
  • react中的forwardRef 和memo的區(qū)別解析

    react中的forwardRef 和memo的區(qū)別解析

    forwardRef和memo是React中用于性能優(yōu)化和組件復(fù)用的兩個高階函數(shù),本文給大家介紹react中的forwardRef 和memo的區(qū)別及適用場景,感興趣的朋友跟隨小編一起看看吧
    2023-10-10
  • useEffect如何通過form.getFieldValue(‘xxx‘)監(jiān)聽Form表單變化

    useEffect如何通過form.getFieldValue(‘xxx‘)監(jiān)聽Form表單變化

    這篇文章主要介紹了useEffect如何通過form.getFieldValue(‘xxx‘)監(jiān)聽Form表單變化問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 在react-router4中進(jìn)行代碼拆分的方法(基于webpack)

    在react-router4中進(jìn)行代碼拆分的方法(基于webpack)

    這篇文章主要介紹了在react-router4中進(jìn)行代碼拆分的方法(基于webpack),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03

最新評論

莱州市| 从化市| 桃源县| 德格县| 榆林市| 咸阳市| 雅江县| 天全县| 江西省| 获嘉县| 改则县| 新乐市| 三穗县| 自贡市| 天全县| 夏津县| 河东区| 花垣县| 辉县市| 富蕴县| 会东县| 桃园县| 阿鲁科尔沁旗| 隆子县| 句容市| 西乌| 永兴县| 德昌县| 屯门区| 佳木斯市| 江华| 吐鲁番市| 龙海市| 信阳市| 尉氏县| 东丽区| 临漳县| 和硕县| 伊宁市| 汶上县| 绥阳县|