lazy-render-virtual-scroll 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -37
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -36,31 +36,31 @@ A framework-agnostic virtual scrolling and lazy rendering solution that efficien
|
|
|
36
36
|
## Installation
|
|
37
37
|
|
|
38
38
|
```bash
|
|
39
|
-
npm install lazy-render
|
|
39
|
+
npm install lazy-render-virtual-scroll
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
## Quick Start -
|
|
42
|
+
## Quick Start - English Guide
|
|
43
43
|
|
|
44
|
-
### React Adapter
|
|
44
|
+
### React Adapter Usage
|
|
45
45
|
|
|
46
46
|
```tsx
|
|
47
47
|
import React, { useState } from 'react';
|
|
48
|
-
import { LazyList } from 'lazy-render';
|
|
48
|
+
import { LazyList } from 'lazy-render-virtual-scroll';
|
|
49
49
|
|
|
50
50
|
const MyComponent = () => {
|
|
51
51
|
const [items, setItems] = useState<any[]>([]);
|
|
52
52
|
const [hasMore, setHasMore] = useState(true);
|
|
53
53
|
|
|
54
|
-
//
|
|
54
|
+
// Function to fetch data
|
|
55
55
|
const fetchMore = async () => {
|
|
56
|
-
// API call
|
|
56
|
+
// Simulate API call
|
|
57
57
|
const newItems = await fetchItems(items.length, 20);
|
|
58
58
|
setItems(prev => [...prev, ...newItems]);
|
|
59
59
|
setHasMore(newItems.length > 0);
|
|
60
60
|
return newItems;
|
|
61
61
|
};
|
|
62
62
|
|
|
63
|
-
//
|
|
63
|
+
// Function to render each item
|
|
64
64
|
const renderItem = (item: any, index: number) => (
|
|
65
65
|
<div style={{ height: '50px', borderBottom: '1px solid #eee' }}>
|
|
66
66
|
Item {index}: {item.name}
|
|
@@ -69,11 +69,11 @@ const MyComponent = () => {
|
|
|
69
69
|
|
|
70
70
|
return (
|
|
71
71
|
<LazyList
|
|
72
|
-
items={items} //
|
|
73
|
-
itemHeight={50} //
|
|
74
|
-
viewportHeight={400} //
|
|
75
|
-
fetchMore={fetchMore} //
|
|
76
|
-
renderItem={renderItem} //
|
|
72
|
+
items={items} // Your data array
|
|
73
|
+
itemHeight={50} // Height of each item
|
|
74
|
+
viewportHeight={400} // Visible height of container
|
|
75
|
+
fetchMore={fetchMore} // Function to fetch data
|
|
76
|
+
renderItem={renderItem} // Function to render items
|
|
77
77
|
bufferSize={5} // Buffer items (extra render)
|
|
78
78
|
overscan={2} // Additional buffer for smooth scrolling
|
|
79
79
|
/>
|
|
@@ -81,11 +81,11 @@ const MyComponent = () => {
|
|
|
81
81
|
};
|
|
82
82
|
```
|
|
83
83
|
|
|
84
|
-
### Hook
|
|
84
|
+
### Hook Usage
|
|
85
85
|
|
|
86
86
|
```tsx
|
|
87
87
|
import React, { useState } from 'react';
|
|
88
|
-
import { useLazyList } from 'lazy-render';
|
|
88
|
+
import { useLazyList } from 'lazy-render-virtual-scroll';
|
|
89
89
|
|
|
90
90
|
const MyCustomComponent = () => {
|
|
91
91
|
const [items, setItems] = useState<any[]>([]);
|
|
@@ -108,21 +108,21 @@ const MyCustomComponent = () => {
|
|
|
108
108
|
}
|
|
109
109
|
});
|
|
110
110
|
|
|
111
|
-
//
|
|
111
|
+
// Extract only visible items
|
|
112
112
|
const visibleItems = items.slice(visibleRange.start, visibleRange.end);
|
|
113
113
|
|
|
114
114
|
return (
|
|
115
115
|
<div
|
|
116
|
-
ref={setContainerRef} //
|
|
116
|
+
ref={setContainerRef} // For scroll detection
|
|
117
117
|
style={{
|
|
118
118
|
height: '400px',
|
|
119
119
|
overflowY: 'auto'
|
|
120
120
|
}}
|
|
121
121
|
>
|
|
122
|
-
{/* Top padding
|
|
122
|
+
{/* Top padding to maintain scroll position */}
|
|
123
123
|
<div style={{ height: `${visibleRange.start * 50}px` }} />
|
|
124
124
|
|
|
125
|
-
{/*
|
|
125
|
+
{/* Render visible items */}
|
|
126
126
|
{visibleItems.map((item, index) => (
|
|
127
127
|
<div
|
|
128
128
|
key={visibleRange.start + index}
|
|
@@ -156,39 +156,39 @@ const MyCustomComponent = () => {
|
|
|
156
156
|
};
|
|
157
157
|
```
|
|
158
158
|
|
|
159
|
-
## Package
|
|
159
|
+
## How Package Works - Step by Step
|
|
160
160
|
|
|
161
|
-
### 1. Install
|
|
161
|
+
### 1. Install
|
|
162
162
|
```bash
|
|
163
163
|
npm install lazy-render
|
|
164
164
|
```
|
|
165
165
|
|
|
166
|
-
### 2. Import
|
|
166
|
+
### 2. Import
|
|
167
167
|
```javascript
|
|
168
|
-
import { LazyList, useLazyList } from 'lazy-render';
|
|
168
|
+
import { LazyList, useLazyList } from 'lazy-render-virtual-scroll';
|
|
169
169
|
```
|
|
170
170
|
|
|
171
171
|
### 3. Basic Usage
|
|
172
|
-
-
|
|
173
|
-
-
|
|
174
|
-
-
|
|
175
|
-
-
|
|
176
|
-
-
|
|
172
|
+
- Get your data array
|
|
173
|
+
- Specify item height
|
|
174
|
+
- Specify container height
|
|
175
|
+
- Provide fetchMore function
|
|
176
|
+
- Provide renderItem function
|
|
177
177
|
|
|
178
178
|
### 4. How It Works Internally
|
|
179
|
-
- **Scroll Detection**:
|
|
180
|
-
- **Range Calculation**:
|
|
181
|
-
- **Smart Rendering**:
|
|
182
|
-
- **Prefetch Logic**:
|
|
183
|
-
- **Memory Cleanup**:
|
|
179
|
+
- **Scroll Detection**: Detects when user scrolls
|
|
180
|
+
- **Range Calculation**: Calculates visible range (which items are visible)
|
|
181
|
+
- **Smart Rendering**: Only renders visible items
|
|
182
|
+
- **Prefetch Logic**: Fetches data before user reaches the end
|
|
183
|
+
- **Memory Cleanup**: Removes off-screen items
|
|
184
184
|
|
|
185
185
|
### 5. Configuration Options
|
|
186
|
-
- `itemHeight`:
|
|
187
|
-
- `viewportHeight`:
|
|
188
|
-
- `bufferSize`: Extra items render (default: 5)
|
|
186
|
+
- `itemHeight`: Height of each item in pixels
|
|
187
|
+
- `viewportHeight`: Visible height of container
|
|
188
|
+
- `bufferSize`: Extra items to render (default: 5)
|
|
189
189
|
- `overscan`: Additional buffer for smooth scrolling (default: 2)
|
|
190
|
-
- `fetchMore`:
|
|
191
|
-
- `renderItem`:
|
|
190
|
+
- `fetchMore`: Function to fetch data
|
|
191
|
+
- `renderItem`: Function to render items
|
|
192
192
|
|
|
193
193
|
### 6. Hook Return Values
|
|
194
194
|
- `visibleRange`: Currently visible items range
|