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

333
Views
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 answers
Answer question

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 Report

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 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!