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

202
Views
State doesn't change instantly on button press in different function in React Native

In my React Native application, i am changing data on previous and next button. Now i have a function called getImageChunked where i'm setting the data on state. But on pressing previous button and next button data doesn't change instantly on those button press functions. Instead in button press functions, it shows the previous state data. Here's my current code :

  const [imageSet, setImage] = useState([]);
  const [count, setCount] = useState(0);
  const [nextImage, setNextImage] = useState(false);
  const [prevImage, setPrevImage] = useState(false);


  useEffect(() => {
      getImageChunked();
    },[]);
      
  nextButton = () =>{
        setCount(count + 1);
        getImageChunked(count + 1);
        console.log('imageset',imageSet) // It shows previous state data
        if (props.imageset === []){
          setNextImage(true);
        }
    }

    prevButton = () =>{
        setCount(count - 1);
        getImageChunked(count - 1);
        console.log('imageset',imageSet) // It shows previous state data
        if (props.imageset === []){
          setPrevImage(true);
        }
        
    }

    getImageChunked = (n) =>{
      var perChunk = 4 

      var result = 'API Data'
      var merged = [].concat.apply([], result[n === undefined ? 0 : n]);
      console.log('merged',merged); // Here the data changes properly
      setImage(merged);
    }

How i can get instant changed state data on prevButton and nextButton press functions?

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

0

From what I understand, React will queue actions and then execute them in batches at a later time, which can cause these types of lagging state issues

One way to deal with this, is to watch your state with useEffect.

useEffect(() => {
  getImageChunked()
}, [count])

This way, getImageChunked() will only be called after your count state has updated

about 4 years ago · Juan Pablo Isaza Report

0

I would suggest following:

  const [imageSet, setImage] = useState([]);
  const [count, setCount] = useState(0);
  const [nextImage, setNextImage] = useState(false);
  const [prevImage, setPrevImage] = useState(false);


  useEffect(() => {
      getImageChunked();
    },[count]); // Each time count is changing getImageChunked got called
      
  nextButton = () =>{
        setCount(count + 1);
        if (Array.isArray(props.imageset)){ // AFAIK props.imageset === [] is always false
          setNextImage(true);
        }
    }

    prevButton = () =>{
        setCount(count - 1);
        if (Array.isArray(props.imageset)){
          setPrevImage(true);
        }
        
    }

    getImageChunked = () =>{
      var perChunk = 4 

      var result = 'API Data'
      var merged = [].concat.apply([], result[count]);
      console.log('merged',merged); // Here the data changes properly
      setImage(merged);
    }
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!