-
Notifications
You must be signed in to change notification settings - Fork 456
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Feature Request
Why it is needed
Performance way to load a LOT of pages dynamically to create a truly dynamic swiper.
Possible implementation / Code sample
Right now I can do something like:
const [position, setPosition] = useState(0);
const onPageSelected = e => {
setPosition(e.nativeEvent.position);
};
const min = 0;
const max = position + numberOfPages;
const items = data.slice(min, max).map((item, index) => (
// If they are are not inside the range, we render null to get a better performance
<View key={item}>
{index < position + numberOfPages && index > position - numberOfPages ? (
<Item item={item} />
) : null}
</View>
));
<ViewPager
onPageSelected={onPageSelected}>
...
>
{items}
</ViewPager>Contraints: List keeps growing while you swipe.
A better way instead of rendering null would be to slice from the beginning too:
const min = position - numberOfPages;
const max = position + numberOfPages;However, this approach has a problem. Consider the scenario:
- Pages:
1 2 3 4and position = 2 (selected element is 3).
We slice from the beginning and we render:
- Pages
2 3 4 5but still position = 2 (selected element will be 4). <-- The problem is that if we change the children in this way, we need to adapt the position (here the position should be 1 for selected element to be still 3).
Another approach would be doing this by default natively: #83.
Bigood, henr, amadogr50, debadaracco, brooksbecton and 13 moretslater, Shadowlong and Skipperlla
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request