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

334
Visualizações
Error: data.map is not a function. This error throws whenever I run react component

I am running HorizontallScrollbar component by passing data as a prop. Whenever I run HorizontalScrollbar component then following error coming in console tab of Chrome:

Error:

Uncaught TypeError: data.map is not a function

HorizontalScrollbar.js

import React from 'react';
import {Box} from '@mui/material';

const HorizontalScrollbar = ({data}) => {
    return (
        <div> 
           {data.map((item) => ( 
            <Box 
            key = {item.id || item}
            itemId = {item.id || item}
            title = {item.id || item}
            m = "0 40px"
            >
            {item}
            </Box>
            )
        )
            
    }
        </div>
)
}

export default HorizontalScrollbar;

Passing data as a Prop:

 <Box sx = {{position: "relative", width: "100%", p: "20px"}}>
     <HorizontalScrollbar data = {bodyParts} />
     </Box>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

How is bodyParts instantiated? Is it a state? If it is it should default to an empty array []

Or you could add a check before mapping through. I added an Array.isArray() check before invoking the map() function as it is only for Arrays.

Additional Info

It could be that data is not yet populated due to waiting for an API call to complete or some other reason. In this case my fix would simply render an empty div during first render due to the check and prevent the console error. During next render when the data is ready the data in this case would be a proper Array type, the check would pass (Array.isArray(data)) and the items would render the Box component properly.

import React from 'react';
import {Box} from '@mui/material';

const HorizontalScrollbar = ({data}) => {
    return (
        <div> 
           {Array.isArray(data) && data.map((item) => ( 
            <Box 
            key = {item.id || item}
            itemId = {item.id || item}
            title = {item.id || item}
            m = "0 40px"
            >
            {item}
            </Box>
            )
        )
            
    }
        </div>
)
}

export default HorizontalScrollbar;
about 4 years ago · Juan Pablo Isaza Relatório

0

I think using the double ampersand && gives it an option.

That is, if API had not been called, remain empty for the time being

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