question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic / lazy approach to load pages

See original GitHub issue

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 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:open
  • Created 4 years ago
  • Reactions:17
  • Comments:25 (10 by maintainers)

github_iconTop GitHub Comments

22reactions
huming-chinacommented, Jan 6, 2020

I really need this feature

18reactions
plrdevcommented, Oct 5, 2021

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found