• Jobs
  • About Us
  • Jobs
    • Home
    • Jobs
    • Courses and challenges
  • Businesses
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

110
Views
here is an API that changes after some time I want previous data to be stored in an array and latest data too I dont how to figure this out?

I want data state to be an array which adds up the data when the data in the API changes such that it stores previous data as well as new data so I can map them and display them

App.js

import React, { useEffect, useState } from "react";

const App = () => {
  const [data, setData] = useState([]);
  const [entireData, setEntireDate] = useState([]);
  

  let arr = [];
  const fetchAPI = async () => {
    const API = await fetch("https://randomuser.me/api");
    const json = await API.json();
    let results = json.results;
    setData(results);
  };

  useEffect(() => {
    fetchAPI();
  }, []);

  useEffect(() => {}, [data]);

  return (
    <>
      <div className="container">
        <p>
          {data.map((val, idx, arr) => {
            return val.login.username;
          })}
        </p>
        <img
          src={data.map((val, idx, arr) => {
            return val.picture.large;
          })}
          alt="getPicFromJson"
        />
      </div>
    </>
  );
};

export default App;

index.js

import { StrictMode } from "react";
import ReactDOM from "react-dom";

import App from "./App";

const rootElement = document.getElementById("root");
ReactDOM.render(
  <StrictMode>
    <App />
  </StrictMode>,
  rootElement
);
over 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

maybe try something like this

  const fetchAPI = async () => {
    const API = await fetch("https://randomuser.me/api");
    const json = await API.json();
    let results = json.results;
    // here you get the old data and pushing the results into Data
    Data.push(results)
    // and setData to Data that has (Data + results)
    setData(Data);
  };

also if you want to have your data after refreshing your browser , you may need to store Data in localstorage and get the initial data from that

over 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Show me some job opportunities
There's an error!