npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yh-react-virtuallist

v0.4.0

Published

yh-react-virtuallist 虚拟列表组件

Downloads

14

Readme

yh-react-virtuallist 虚拟列表组件

build

  • demo: https://yehuozhili.github.io/yh-react-virtuallist/

制作初衷

我以前写的虚拟滚动实现的并不是很好,后来用 umihook 发现也不是很好用,它支持手动设定每个元素高度,但也不能支持不定高度,而且限定更多了,比如不能在同一个滚动 dom 下绑定多个虚拟滚动,对跨组件调用不太友好,甚至第一次出现可能不会显示,需要划一下或者使用 scrollto 才会出现。于是想自己制作个。

特点

  • 可以自动获取高度的虚拟列表组件!
  • 传入 3 个参数即可工作!

快速上手

  • VirtualList 是必须指定高的。SuperVirtualList 不用指定高,但需要给个估算值。
npm i yh-react-virtuallist
import React, { useRef } from "react";
import VirtualList, { SuperVirtualList } from "yh-react-virtuallist";

const mockArr = new Array(10000).fill(1).map((x, y) => y);

export function VirtuallistDemo() {
	const ref = useRef<HTMLDivElement>(null);
	return (
		<div
			ref={ref}
			style={{
				height: "500px",
				border: "1px solid black",
				overflow: "auto",
			}}
		>
			this text in the current component
			<div>
				<VirtualList renderNumber={25} itemHeight={30} scrollDom={ref}>
					{mockArr.map((v, i) => {
						return (
							<div
								style={{
									border: "1px solid black",
									height: "30px",
								}}
								key={i}
							>
								{v}
							</div>
						);
					})}
				</VirtualList>
			</div>
		</div>
	);
}

export function SuperVirtuallistDemo() {
	const ref = useRef<HTMLDivElement>(null);
	return (
		<div
			ref={ref}
			style={{
				height: "500px",
				border: "1px solid black",
				overflow: "auto",
			}}
		>
			<div>
				<SuperVirtualList
					scrollDom={ref}
					referItemHeight={25}
					renderNumber={30}
				>
					{mockArr.map((v, i) => {
						return (
							<div
								style={{
									border: "1px solid black",
									height: `${Math.random() * 30 + 20}px`,
								}}
								key={i}
							>
								{v}
							</div>
						);
					})}
				</SuperVirtualList>
			</div>
		</div>
	);
}

参数

export interface VirtualListProps extends AllHTMLAttributes<HTMLDivElement> {
	//滚动dom的ref
	scrollDom: RefObject<HTMLDivElement>;
	//每个元素的高度
	itemHeight: number;
	//渲染出几个元素
	renderNumber: number;
}
export interface VirtualListProps extends AllHTMLAttributes<HTMLDivElement> {
	scrollDom: RefObject<HTMLDivElement>;
	referItemHeight: number; //这个是估算高度,用来一开始估算大致高度。
	renderNumber: number;
}