Jacse / react-native-app-intro-slider

Simple and configurable app introduction slider for react native
MIT License
1.65k stars 330 forks source link

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

Open pgrepds opened 2 years ago

pgrepds commented 2 years ago

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 commented 2 years ago

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

wicfasho commented 1 year ago

You do not need to create a custom index state.

Follow this example: https://github.com/Jacse/react-native-app-intro-slider/issues/224#issuecomment-1367099899