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

122
Views
react js How to keep the initial data saved after applying the fitler on array

I am working on a React JS based UI and facing an issue wiht data .

  1. I fetch data from an API and store it in an initial value by using useState hook.
  2. Then I use useEffect to store it in 'lists' via setLists()
const [lists, setLists] = useState([]);
useEffect(()=>{
  
  const url = "https://domain/api/all";
  fetch(url).then(response => response.json()).then(response=> {
    setLists(response.json.list);
  });
  1. Now I am able to use map to enter data in a table
  2. Lists is an array object that has multiple values in it and I want t filter the table based on age and country
let [ageList, setAgeList] = useState([]);
let [countryList, setcoutryList] = useState([]);

useEffect(()=>{
 var newList = lists.filter(e=> e.age === age); 
  setList(newList)

},[age])


useEffect(()=>{

  setlist(lists.filter(e=> e.country === country))

},[country])

With this method first attempt of filter works fine by both Age and Country but when I change the value of the filter for age or country then I get empty array which is expected as the new lists variable has entered with last filtered country/age

How can I get over this?

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

0

Do you have control over the fetched data? If so, the obvious solution would be to change the datamodel server side.

If not, I would suggest storing the initial response in a mutable object from useRef.

Edit

const [lists, setLists] = useState([]);
const cache = useRef([]);

useEffect(()=>{
  
  const url = "https://domain/api/all";
  fetch(url).then(response => response.json()).then(response=> {
    cache.current = response.json.list;
    setLists(response.json.list);
  });
});
about 4 years ago · Juan Pablo Isaza Report

0

As you noticed, when you overwrite your previous state with the filtered state, you lose the previous state forever. The solution here is to keep the initial value of lists and create a new variable for your filtered lists. Something like this:

const [lists, setLists] = useState([]);
const [ageFilter, setAgeFilter] = useState("");
const [countryFilter, setcountryFilter] = useState("");

const url = "https://domain/api/all";

fetch(url)
  .then(response => response.json())
  .then(response=> {
    setLists(response.json.list);
  });

let filteredByCountryAndAge = listfilter((listItem) => {
  return (listItem.country === countryFilter && listItem.age === ageFilter);
});

(or however else you are filtering it).

The point being, do not change the value of lists after it is set with the entire list. When filtering, assign the criteria by which you are filtering to state variables, but assign the filtered data to a variable—NOT a state variable.

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!