forked from petyosi/react-virtuoso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepend.tsx
54 lines (50 loc) · 1.41 KB
/
prepend.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { default as React, useRef, useState } from 'react'
import * as ReactDOM from 'react-dom'
import { Virtuoso } from '../src/'
const User = React.memo(({ user }) => {
console.log(`rendering user ${user.index}`)
return <span>User {user.index}</span>
})
const App = () => {
const virtuoso = useRef(null)
const initialIndexOffset = useRef(10000)
const [users, setUsers] = useState(
Array(200)
.fill(true)
.map((_, index) => ({
index: 10000 + index,
}))
)
const prependItems = React.useCallback(() => {
const usersToPrepend = 100
initialIndexOffset.current -= usersToPrepend
setUsers([
...Array(usersToPrepend)
.fill(true)
.map((_, index) => ({ index: initialIndexOffset.current + index })),
...users,
])
virtuoso.current.adjustForPrependedItems(usersToPrepend)
return false
}, [initialIndexOffset, users, setUsers])
return (
<div style={{ display: 'flex' }}>
<div>
<Virtuoso
ref={virtuoso}
totalCount={users.length}
item={index => <User user={users[index]} />}
style={{ height: '400px', width: '350px' }}
/>
</div>
<div>
<ul className="knobs">
<li>
<button onClick={prependItems}>Prepend 100 items</button>
</li>
</ul>
</div>
</div>
)
}
ReactDOM.render(<App />, document.getElementById('root'))