Dynamic / lazy approach to load pages
See original GitHub issueFeature 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 4
and position = 2 (selected element is 3).
We slice from the beginning and we render:
- Pages
2 3 4 5
but 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: https://github.com/react-native-community/react-native-viewpager/issues/83.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:17
- Comments:25 (10 by maintainers)
Top Results From Across the Web
Lazy Loading: How It Decreases Load Time and Increases ...
Lazy loading is a technique used by web pages to optimize load time. With lazy loading, a web page loads only required content...
Read more >What is Lazy Loading? - StackPath
Dynamically loading resources allows the user to explore more content without having to jump across multiple pages or wait for their browser to...
Read more >Lazy Loading Images – The Complete Guide - ImageKit.io
Lazy Loading Images is a set of techniques in web and application development that defer the loading of images on a page to...
Read more >Make Webpages Load Faster with Lazy Loading - Infinum
Lazy loading is a technique that defers the loading of non-critical resources during the page load time. Instead, these non-critical ...
Read more >React Lazy Loading: The Best Complete Guide - CopyCat Blog
For example, if a web page has an image that the user has to scroll down to see, you can display a placeholder....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I really need this feature
Hey this has been as RC release for quite some time, with many regular releases after it. Are there still some issues with this or what is the reasoning not making a non-RC release with lazy pager included? I am wondering whether to try this feature out, but I would rather wait if it is still considered not release ready. Thanks!