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

147
Views
Get data from API, store in state, then splice the data into a new state - React

I'm trying to get data from an API, and store that in setData(response.data). Then I'm trying to splice that data into a new state called items. So I do setItems(data.events?.splice(lastPage, firstPage));

Somehow my items array initially is always empty, I need to refresh the site to get the info. I'm using a loading state for that, but for some reason my items don't refresh it's state. This is the code

 const [data, setData] = useState([]);
 const [isLoading, setIsLoading] = useState(true);

 const [items, setItems] = useState([]);
 const [page, setPage] = useState(1);
 const EVENTS_PER_PAGE = 10;
 const firstPage = page * EVENTS_PER_PAGE;
 const lastPage = firstPage - EVENTS_PER_PAGE;

 useEffect(() => {
   getEvents()
     .then(function (response) {
       setData(response.data);
       setItems(data.events?.splice(lastPage, firstPage));
     })
     .catch(function (error) {
       setError(error);
     })
     .finally(function (response) {
       setIsLoading(false);
     });
 }, []);

JSON: https://www.mockachino.com/c31972ec-27f7-45/events

I tried everything but I can't make it work, any help is appreciable

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

0

When you are trying to put data in state, you cannot access its new value in the same useEffect.

What you can do is:

  1. First useEffect only fetches data and put it in state;
  2. Second useEffect (you need to create it) changes state of your items
useEffect(() => {
   getEvents()
     .then(function (response) {
       setData(response.data);
     })
     .catch(function (error) {
       setError(error);
     })
     .finally(function (response) {
       setIsLoading(false);
     });
 }, []);
useEffect(() => {
  setItems(data.events?.splice(lastPage, firstPage));
}, [data, lastPage, firstPage])

Second useEffect will fire when [data] array changes.

about 4 years ago · Juan Pablo Isaza Report

0

1=> do not render the page until isLoading is false something like

return isLoading ? (
    <> spinner or some else < />
  ) : (
    <> your page </>

that way your page will not render until the data is loaded.

2=> also try adding the isLoading variable to the dependency array so that page re-renders when isLoading changes

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!