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

Lazy loading the components in viewpager #677

Open
tahadar16 opened this issue Jan 6, 2023 · 3 comments
Open

Lazy loading the components in viewpager #677

tahadar16 opened this issue Jan 6, 2023 · 3 comments
Labels
question Further information is requested

Comments

@tahadar16
Copy link

tahadar16 commented Jan 6, 2023

Lazy loading of components

I am unable to understand how do I stop the viewpager from loading all of my components at the same time. I want when the user selects a category and switches to that particular screen thats when it gets loaded. But right now its loading all of the components altogether

@tahadar16 tahadar16 added the question Further information is requested label Jan 6, 2023
@ttschnz
Copy link

ttschnz commented Jan 8, 2023

My interpretation of the docs is that offscreenPageLimit should do this, but I couldn't see it working on my app.

I found a workaround by returning an empty view on these items that are further away than x scrolls.
Consider this example having 10 screens that count from 0 to 9:

function Screen() {
    const startIndex = 4;
    let [currentIndex, setCurrentIndex] = useState(startIndex);
    const renderLimit = 2; // render 2 pages before and after the current page
    return (
        <PagerView
            initialPage={startIndex}
            style={{ flex: 1 }}
            onPageSelected={(event) => {
                setCurrentIndex(event.nativeEvent.position);
            }}
        >
            {Array.from(Array(10).keys()).map((i, index) => {
                return (
                    <View
                        style={{
                            alignItems: "center",
                            backgroundColor: "white",
                            flex: 1,
                            justifyContent: "center",
                        }}
                        key={i}
                    >
                        {Math.abs(currentIndex - index) < renderLimit && (
                            <Text>{i}</Text>
                        )}
                    </View>
                );
            })}
        </PagerView>
    );
}

https://snack.expo.dev/@ttschnz/lazy-loading-the-components-in-viewpager

@tahadar16
Copy link
Author

tahadar16 commented Jan 16, 2023

I believe offscreenPageLimit works only for the Android platform as per the documents. I did see a few pull requests regarding the lazy loading but can't find any public methods in the repo for this requirement

Check this --> #331
and this as well --> https://github.com/callstack/react-native-pager-view/releases/tag/v6.0.0-rc.0

@abumalick
Copy link

See the work around in #673 (comment)

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

3 participants