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

onSlideChange is fired twice on state change with initial index #259

Open
pgrepds opened this issue Mar 28, 2022 · 2 comments
Open

onSlideChange is fired twice on state change with initial index #259

pgrepds opened this issue Mar 28, 2022 · 2 comments

Comments

@pgrepds
Copy link

pgrepds commented Mar 28, 2022

I want to call goToSlide using a ref to the AppIntroSlider and I want to save the current index in a state. Consider the following example.

const [index, setIndex] = useState(0)
const ref = useRef(null)

function onSlide(idx) {
    setIndex(idx)
}

function MyCustomButton(props) {
    return <Button title="Next" onPress={() => ref.current.gotToSlide(index + 1, true)} />
}

return (
    <AppIntroSlider<ScreenItem>
           ref={ref}
           onSlideChange={onSlide}
           renderNextButton={() => <MyCustomButton />}
           renderItem={(...) => ...}
)

If I press MyCustomButton, then onSlide is called with the correct index 1. The setIndex causes a rerender and it seems that the onSlideChange function is called again with the old initial index 0.

Is there a way to make this setup work or is this behavior by design?

Version: 4.0.4

@theafolayan
Copy link

I am facing the exact Issue... Anyone able to solve this?

@wicfasho
Copy link

You do not need to create a custom index state.

Follow this example: #224 (comment)

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

3 participants