Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda