0.0.1-alpha.1 • Published 1 year ago

react-waterfall-virtual-list v0.0.1-alpha.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

react-waterfall-virtual-list

适用于移动端的瀑布流虚拟列表

install

npm i react-waterfall-virtual-list -S

基本使用

和 antdMoblie 的 PullToRefresh 和 InfiniteScroll 搭配使用

export default function Demo() {
  const [data, setdata] = React.useState([]);
  const VirtualListInstance = React.useRef();

  const loadMore = async () => {
    const reslult = await mockGoods();
    const newData = [].concat(data, reslult);
    if (Math.random() < 0.05) {
      throw new Error("mock request failed");
    }
    setdata(newData);
  };

  const onRefresh = async () => {
    const reslult = await mockGoods();
    await VirtualListInstance.current.reset();
    setdata(reslult);
    resolve();
  };

  return (
    <PullToRefresh onRefresh={onRefresh}>
      <VirtualWaterfallList
        style={{
          boxSizing: "border-box",
          height: "calc(100vh - 12px)",
        }}
        ref={VirtualListInstance}
        dataSource={data}
        showCount={10}
        gap={4}
        footer={<InfiniteScroll loadMore={loadMore} hasMore={true} />}
        ItemRender={React.memo(({ index, item, className, ...props }) => {
          return (
            <div
              className={`van-demo-goods-item-wrapper ${className}`}
              {...props}
            >
              <div className="van-demo-goods-item">
                <img src={item.image} className="img" />
                <div className="title">{item.title}</div>
                {item.isCutPrice && (
                  <span className="cutPrice">最近大降价</span>
                )}
                <div className="price">{item.price}</div>
              </div>
            </div>
          );
        })}
      />
    </PullToRefresh>
  );
}

Demo online

在线查看

API

props

参数说明类型默认值必填
listStyle列容器的样式  CSSProperties-false
listClssName列容器的样式名  string-false
height滚动外层容器高度  number ¦ string-true
footer底部额外渲染  ReactNode-false
showCount可视区域展示的最大数量, 高度不一的时候按全部最小高度展示去计算  number-true
gap可视区域展示的最大数量  number-false
dataSource数据源,数组  Array-true
ItemRender自定义渲染每一项  FunctionComponent<    {      item: T      index?: number    } & ViewProps  >-true
renderBackToTop自定义回到顶部按钮渲染  ReactNode-false
backToTopSuccess成功返回顶部后执行  () => void-false
backToTopCritical展示返回顶端按钮的临界值,上方隐藏了多少个 ItemRender  numberVirtualHalfList的为showCount乘 2,VirtualWaterfallList的为showCountfalse

组件实例

方法说明类型
reset重置状态  () => void
backToTop返回顶部  () => void