leecade / leecade/react-native-swiper

index value will be out of reange after updating the UseState

Open
#1,368 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
10.5k
Forks
2.3k
PR merge metrics
No merged PRs in 30d

Description

Which OS ?

Android

  • react-native-swiper v?

"react-native-swiper": "^1.6.0",

Expected behaviour

there are 3 buttons, when the button is clicked the "activeComponent " state is updated, it must update the index of the swiper based on the activeComponent state. and when we swipe it must work normally from the updated index.

Actual behaviour

swiper works normally in the beginning.
even the clicking on the button changes the index of the swiper.
But after clicking on the button if we swipe we get the unexpected behaviour.when we swipe we get the blank screen.whene we swipe again we will be jumped to the another screen etc.

How to reproduce it>

To help us, please fork this component, modify one example in examples folder to reproduce your issue and include link here.

const [activeComponent, setActiveComponent] = useState("teachingRequests");
<Pressable
onPress={() => {
setActiveComponentAndResetPage("zero");
}}
>

zero

<Pressable
onPress={() => {
setActiveComponentAndResetPage("one");
}}
>

one


<Pressable
onPress={() => {
setActiveComponentAndResetPage("two");
}}
>

two




<Swiper
showsButtons={false}
showsPagination={false}
loop={false}
horizontal={true}
index={
activeComponent === "zero"
? 0
: activeComponent === "one"
? 1
: 2
}
onIndexChanged={(index) => {
const components = [
"zero",
"one",
"two",
];

        console.log(index);
        console.log(components[index])

        setActiveComponent(components[index]);
        
        if (components[index] === "zero") {
          setCurrentPage(1);
          ZeroHandler(1);
        } else if (components[index] === "one") {
          setCurrentPage(1);
          OneHandler(1);
        } else if (components[index] === "two") {
          setCurrentPage(1);
          TwoHandler1);
        }
      }}
    >
Steps to reproduce

1.swipe to the last scrollview
2.then click on first button "zero"
3. then swipe u will get the blank screen
4. the console log of the index will be in negative and sometimes will be out of range as here the index must be 0,1,2 but it goes to 3, -1 , -2 etc
5. this is console log
LOG 1
LOG one
//above is the first right swipe
LOG 2
LOG two
//above is second right swipe
LOG 0
LOG zero
//above log is when clicked on zero button
LOG -1
LOG undefined
LOG 2
LOG two
//above log is when swiped right

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Swiper usage described in an example under the examples folder, focusing on the index prop and onIndexChanged callback. Reproduce the button-driven index change followed by a swipe on Android, then inspect the component behavior around those entry points. Done means the index remains within 0, 1, and 2 and swiping continues from the selected screen without blank screens.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react-native
Domain
mobile
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.