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

193
Visualizações
React Redux fetch data before render and do a map function

//the website render before i fetch the data from redux and give me a error //TypeError: Cannot read properties of undefined (reading 'map')

const Home=()=> {

const dispatch = useDispatch();

//import the products list
const getProducts =  useSelector ((state) => state.getProducts);
const  { products,users, loading, error}= getProducts; //i fetch the data

const AllUsersData = [ ...(users.map(item => item.createdAt))];
 console.log(AllUsersData);

console.log(users);

useEffect(() => {
    dispatch(listProducts());
  }, [dispatch]);
  
return (
    <div className="home">
        <FeaturedInfo/>
        {loading ? (
        <h2>loading...</h2>
      ) : error ? (
        <h2>{error}</h2>
      ) : (
        <Chart data={AllUsersData} title="User Analytics" grid dataKey="Active User"/>
        )
      }
        <div className="homeWidget">
            <Widgetsmall/>
            <Widgetlg/>
        </div>
    </div>
)

}

export default Home

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

users is expected to be an array since you are using map function. You will need to either:

  1. Set a default value:
const  { products = [],users = [], loading, error} = getProducts;

OR

  1. Check if the value is defined; then do the map function:
const AllUsersData = users ? [ ...(users.map(item => item.createdAt))] : [];
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