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

changing parent component state within onPageChange prop results in undesired behavior #29

Open
kaloncheung124 opened this issue Jul 26, 2017 · 1 comment

Comments

@kaloncheung124
Copy link

In my app, we have a screen component that contains the carousel and a list of data. We wish to use the carousel onPageHandler to select between different sources for the data. However, when
we try to change the screen component state within onPageChange, the carousel pauses and then scrolls back to the first element.

Here is the stripped down code for our home screen with the carousel within it:

export default class HomeScreen extends React.component {

    constructor(props) {
        super(props);
        this.state = {currPageNum: 0};
    }

    ...

    render() {
        return (
            <View>
                <Carousel
                    initialPage={0}
                    pageStyle={{
                        flex: 1,
                        flexDirection: 'column',
                        alignItems: 'center',
                        justifyContent: 'flex-start',
                        padding: 5,
                        backgroundColor: '#f7f7f7',
                    }}
                    sneak={0}
                    swipeThreshold={0.25}
                    onPageChange={(page) => {
                        console.log(page);
                        this.setState({currPageNum: page})
                     }}
                >
                    {...}
                </Carousel>
            </View>
        );
    }

Any help would be greatly appreciated!

@kaloncheung124
Copy link
Author

I think it's due to the currentPage default prop. If I haven't set the currentPage prop, whenever componentWillReceiveProps gets called, it still receives a currentPage prop with a value of 0. This sets this.state.currentPage to 0 whenever the carousel is re-rendered. Perhaps remove this from the default props and add some checking for undefined?

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

No branches or pull requests

1 participant