react-native-pager-view icon indicating copy to clipboard operation
react-native-pager-view copied to clipboard

Prevent rendering all but the active view?

Open rtlayzell opened this issue 8 months ago • 2 comments

Hi, first of all, thanks for the great work!🙂

I want to use your PagerView to create a sign-up/onboarding process with a number forms one after the other. It works well but I notice that it renders all of the offscreen views as well and rendering all those components is making my app sluggish. I tried setting offscreenPageLimit to 1 but it still renders all six of my pages.

Would it be possible to somehow render only the active page? As moving between pages is expected to be relatively infrequent when compared to other uses, it would be nice to optimize it in this way.

If not, do you have any recommendations for alternative components/libraries I could use to achieve this? I don't want to use the expo-router as the slide in animation is slow and lags.

Edit: It may not be entirely a rendering issue, as I noticed if I don't move between the pages, only the active page is rendered. But for some reason, performance degrades with the number pages and the content within them. The same performance issues weren't there in something like react-native-swiper (which I believe is built on top of a ScrollView 😅) and it's not the pages themselves as individually rendered they're performant.

rtlayzell avatar May 20 '25 16:05 rtlayzell

So I managed to improve my performance issues by lazy-loading the components for the activePage and pages either side of it by conditionally rendering them. Feels like the offscreenPageLimit should be doing this, but I don't think it is. There was a slight issue with this approach, a little stutter in the slide-in animation because the onPageSelected - which I'm using to update the activePage - occurs before the animation has completed, thus mounting/unmounting my pages. I resolved this by just creating a delayedActivePage state and a useEffect on activePage to trigger a timeout that would set the delayedActivePage to activePage. Works buttery smooth now 🙂

rtlayzell avatar May 22 '25 10:05 rtlayzell

I ended up using a horizontal ScrollView that only renders adjacent pages and uses placeholders for the rest. It's very snappy.

silvestreh avatar Jul 31 '25 00:07 silvestreh