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

397
Visualizações
Component not rendering after refresh , only after changing and saving the code

I'm having an issue when i'm trying to present a list of countries that i get from the people array i have from the localStorage.

When i refresh the page I do not receive the list ,but when I change something in my code and save it, i do get the list.

I have added the code ,I'm referring to the function getCountries inside of the useEffect where i'm getting all of the countries,getting a unique array sorting it and then saving it to the state.

appreciate any help

import React, { useEffect, useState } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { loadPeople, removePerson, setFilterBy } from '../cmps/store/people.action.js';
import { PeopleList } from '../cmps/PeopleList';
import Filters from '../cmps/Filters.jsx';

export const RecipePage = () => {
  const [currentPage, setCurrentPage] = useState(1);
  const [postsPerPage] = useState(6);
  const [countries, setCountries] = useState([]);

  const peoples = useSelector((state) => state?.peopleModule?.peoples);
  const dispatch = useDispatch();

  useEffect(() => {
    const getCountries = async () => {
      const countryArray = await peoples.map((person) => {
        return person.location.country;
      });
      const uniqueCountryArray = [...new Set(countryArray)];
      const sortedArray = uniqueCountryArray?.sort((a, b) => a.localeCompare(b));
      setCountries(sortedArray);
    };

    getCountries();
  }, []);

  useEffect(() => {
    dispatch(loadPeople());
  }, []);

  const onHandleRemovePerson = (id) => {
    dispatch(removePerson(id));
  };

  const onChangeFilter = (filterBy) => {
    dispatch(setFilterBy(filterBy));
    dispatch(loadPeople());
  };


  return (
    <>
      <div className='main-layout'>
        <Filters countries={countries} onChangeFilter={onChangeFilter} />
        <PeopleList peoples={currentPost} onHandleRemovePerson={onHandleRemovePerson} />
        <Pagination postsPerPage={postsPerPage} totalPosts={peoples.length} paginate={paginate} />
      </div>
    </>
  );
};
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Try adding peoples as dependency

  useEffect(() => {
    const getCountries = async () => {
      const countryArray = await peoples.map((person) => {
        return person.location.country;
      });
      const uniqueCountryArray = [...new Set(countryArray)];
      const sortedArray = uniqueCountryArray?.sort((a, b) => a.localeCompare(b));
      setCountries(sortedArray);
    };

    getCountries();
  }, [peoples]);
about 4 years ago · Juan Pablo Isaza Relatório

0

when sorting and setting the array, state doesnt understand it as a change, when you use spread operator it will work fine.

setCountries([...sortedArray]);
about 4 years ago · Juan Pablo Isaza Relatório

0

I also faced similar situation when page was not showing list on refresh.

Later I figured out that, I missed to mention the states on which page should be rendered again.

Ex:

useEffect(() => {


},[state1, state2]);

This solved my issue and I got the list.

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