Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What happened to LazyPagerView? #801

Closed
MoritzCooks opened this issue Dec 15, 2023 · 5 comments
Closed

What happened to LazyPagerView? #801

MoritzCooks opened this issue Dec 15, 2023 · 5 comments
Labels
question Further information is requested

Comments

@MoritzCooks
Copy link

Ask your Question

I just found the issue #227 about preventing render for all pages at once. In the issue there is a PR mentioned that provides LazyPagerView component and it's also reference to 6.0.0-rc but it's not part of version 6.2.3

Is this available now or not (any more)?

Currently I have to prevent rerendering of all pages.

Thanks for clarification.

@MoritzCooks MoritzCooks added the question Further information is requested label Dec 15, 2023
@troZee
Copy link
Member

troZee commented Dec 15, 2023

@MoritzCooks hope it clarifies the situation #673.
If you would like to achieve the lazy effect, you can use this library https://reactnavigation.org/docs/tab-view/ and hide the tabs.

@troZee troZee closed this as completed Dec 21, 2023
@MoritzCooks
Copy link
Author

MoritzCooks commented Jan 15, 2024

Thanks for the clarification!
I actually found a workaround for that 🙂

@dareman-venkat
Copy link

@MoritzCooks Could you please share the workound for the lazy loading for not rendering all compoents

@efstathiosntonas
Copy link

@MoritzCooks would love to see the workaround too, can you please post an example? Thanks!

@MoritzCooks
Copy link
Author

MoritzCooks commented May 27, 2024

Oh sorry for responding so late @dareman-venkat and @efstathiosntonas .

I just adjusted the implementation to be a bit easier to understand. But that's basically it, just a bit stripped down:

const [page, setPage] = useState(0);

return (
  <PagerView
    offscreenPageLimit={2}
    initialPage={0}
    onPageSelected={({ target }) => setPage(target)}
  >
    {screens?.map((screen, index) => (
      <OnboardingScreen
        key={screen.id}
        shouldRender={index >= page - 1 && index <= page + 1}
        {...screen}
      />
    ))}
  </PagerView>
);

shouldRender

The children have a custom shouldRender prop which accepts a boolean.

  • If true, render the component the way it should.
  • If false, return as early as possible within the child: if (!shouldRender) return null

To check whether shouldRender is true or false, I keep track of the current page outside of the PagerView in a state.
In addition I add a tolerance of one page forward and backwards to prerender the next screen in both directions before it gets shown.

I hope this still helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants