Skip to content

Commit cb2990b

Browse files
committed
feat: 🎸 preload items 100px before the reach end
1 parent 0768958 commit cb2990b

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

‎src/InfiniteScroll/__story__/story.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ const Block = () => {
1515

1616
class Demo extends React.Component {
1717
state = {
18-
items: [],
18+
items: [
19+
<Block key={0} />,
20+
<Block key={1} />,
21+
<Block key={2} />,
22+
<Block key={3} />,
23+
<Block key={4} />,
24+
],
1925
cursor: 1,
2026
};
2127

2228
constructor (props) {
2329
super(props);
24-
this.load();
2530
}
2631

2732
load = (cnt = 5) => {

‎src/InfiniteScroll/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface InfiniteScrollProps {
99
cursor?: number | string;
1010
sentinel?: React.ReactElement<any>;
1111
hasMore?: boolean;
12+
margin?: number;
1213
loadMore: () => void;
1314
}
1415

@@ -19,6 +20,7 @@ export class InfiniteScroll extends React.Component<InfiniteScrollProps, Infinit
1920
static defaultProps = {
2021
sentinel: defaultSentinel,
2122
hasMore: true,
23+
margin: 100,
2224
};
2325

2426
lastLoadMoreCursor: number | string | null = null;
@@ -34,11 +36,11 @@ export class InfiniteScroll extends React.Component<InfiniteScrollProps, Infinit
3436

3537
render () {
3638
const {props} = this;
37-
const {children, hasMore, sentinel} = props;
39+
const {children, hasMore, sentinel, margin} = props;
3840
return h(React.Fragment, null,
3941
children,
4042
hasMore &&
41-
h(ViewportScrollSensor, {onChange: this.onViewportChange}, () => sentinel),
43+
h(ViewportScrollSensor, {margin: [0, 0, margin, 0], onChange: this.onViewportChange}, sentinel),
4244
);
4345
}
4446
}

0 commit comments

Comments
 (0)