tcs-react-virtualize-data
Version:
improve performance when render html with data
127 lines (102 loc) ⢠4.38 kB
Markdown
š **Effortlessly Render Large Datasets Without Slowing Down Your UI!**
**tcs-react-virtualize-data** is a lightweight React library that **renders only visible items**, improving performance **without affecting your UI**. It works seamlessly with tables, grids, lists, and more.
---
- **š Easy Integration** ā Simple setup with React.
- **šØ Seamless Integration** ā Works with any existing design.
- **š Universal Support** ā Compatible with lists, tables, and grids.
- **š Optimized Rendering** ā Reduces DOM updates for a smoother experience.
- **š High Performance** ā Build for Optimized for large datasets.
---
Install the package via npm or yarn:
```bash
npm install tcs-react-virtualize-data
yarn add tcs-react-virtualize-data
```
---
Import and use the hook in your React project:
```tsx
import useVirtualizeData from "tcs-react-virtualize-data";
import { moviesData } from "./testData.tsx";
import { Waypoint } from "react-waypoint";
import YourItem from "./MovieItem.tsx";
export default function MyComponent() {
// Using useVirtualizeData to handle virtualized pagination
const {
data: items, // The currently visible items based on pagination
goNext, // Function to load the next page of data
goBack, // Function to load the previous page of data
} = useVirtualizeData({
data: moviesData, // Large dataset
itemsPerPage: 30, // Items per page (default: 30)
storeAmountOfPages: 2, // Pages kept in memory (default: 2)
});
return (
<div>
{/* Rendering virtualized items */}
{items.map((value, index, array) => (
<ItemWithReactWayPoint
key={value.id}
item={value}
onScrollButton={goNext} // Triggered when scrolling down
onScrollTop={goBack} // Triggered when scrolling up
haveWayPoint={index === 0 || index === array.length - 1}
// Add Waypoint to first & last items
/>
))}
</div>
);
}
/**
* Component to render individual items, optionally including a Waypoint
* for triggering pagination when scrolling up or down.
*/
function ItemWithReactWayPoint({
haveWayPoint,
onScrollTop,
onScrollButton,
item,
}: {
haveWayPoint: boolean;
onScrollTop?: () => void;
onScrollButton?: () => void;
item: any;
}) {
if (haveWayPoint) {
return (
<Waypoint
onEnter={(args) => {
// Load the next set of data when reaching the bottom
if (args.previousPosition === "below" && onScrollButton) {
onScrollButton();
}
// Load previous data when reaching the top
if (args.previousPosition === "above" && onScrollTop) {
onScrollTop();
}
}}
>
<YourItem movie={item} />
</Waypoint>
);
}
return <YourItem movie={item} />;
}
```
| Prop | Type | Default | Required | Description |
|----------------------|----------|---------|----------|----------------------------------------------------|
| `data` | `Array<any>` | - | ā
Yes | The list of items to be virtualized. |
| `itemsPerPage` | `number` | `30` | ā No | Number of items to display per page. |
| `storeAmountOfPages`| `number` | `2` | ā No | Number of pages to keep in memory to optimize rendering. |
ISC License Ā© [Taing chheangseng](https://github.com/Chheangseng)
Made with ā¤ļø in Cambodia š°š
---
š **Contributions & Issues**
Have a suggestion or found a bug? Feel free to [open an issue](https://github.com/Chheangseng/tcs-react-virtualize-data/issues) or contribute!
Let me know if you need further tweaks! š