Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
When cycling through options in a list the only items displayed is the last option (displayed twice) and the second to last option (react native JS)

I am trying to cycle through options in a list using left and right buttons that call the functions cycleLeft() and cycleRight(), but instead all I get is the second to last option displayed twice in a row and then the last option displayed once. this is for a react native app inside the main app function. any help is appreciated.

var gamesList = ["Quadratic", "Magic Square", "Settings", "Option 3"];
var index = 0;
var game = gamesList[index];

const [gameText, setGameText] = useState(gamesList[index]);
const [gameGame, setGameGame] = useState(gamesList[index]);
const zAnim = useRef(new Animated.ValueXY({ x: 0, y: 1000 })).current;

const cycleLeft  = () =>  {
  index = index - 1;
  if (index < 0) { index = gamesList.length - 1; };
  game = gamesList[index];
  setGameText(gamesList[index]);
  console.log(game + " " + index);
}

const cycleRight = () => {
  index = index + 1;
  if (index > gamesList.length - 1) { index = 0; };
  game = gamesList[index];
  setGameText(gamesList[index]);
  console.log(game + " " + index);
}

Console Log

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

index should be stateful instead of gameText

var gamesList = ["Quadratic", "Magic Square", "Settings", "Option 3"];
const [index, setIndex] = useState(0);

const cycleLeft  = () =>  {
  setIndex(prevIndex => prevIndex - 1 < 0 ? gamesList.length - 1 : prevIndex - 1);
}

const cycleRight = () => {
  setIndex(prevIndex => prevIndex + 1 > gamesList.length - 1 ? 0 : prevIndex + 1);
}

//just for logging
useEffect(() => {
  console.log(gamesList[index]+ " " + index);
}, [index]);

Wherever you were using gameText, you can just use gamesList[index] instead.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!