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

237
Views
TypeError: null is not an object (evaluating 'daysData.map') - Trying to map over array of objects

Hope you're all doing good, I've tried looking around for a specific answer regarding this issue I'm having in ReactJS where I'm trying to map over an array I'm fetching via my local system and I keep getting this error in the console:

TypeError: null is not an object (evaluating 'daysData.map')

Here is a snapshot of my bit of code where I'm initializing the array from fetch: Fetched data to Array

And here it is in text format:

// Here I initalize the array with useState
  const [daysData, setDaysData] = useState(null);

  // Here is the port I'm fetching my array from.
  useEffect(() => {
    fetch('http://localhost:5001/chocolates')
      .then((resp) => resp.json())
      .then((data) => setDaysData(data));
  }, []);

  // Console logging is sending back the array with no issues
  console.log(daysData);

And here is the map function where I'm trying to render a with the 'id' key from the array:

Array.map not working

Here is the bit of code in text format:

{/* Here I'm getting the error " TypeError: null is not an object (evaluating 'daysData.map') " */}
      {daysData.map((day) => (
        <div>{day.id}</div>
      ))}

And here is an example of the array where I've fetched data from in which I'm trying to map over: Array and Objects Within

Thanks for any help in advance

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Just set the default state to an empty array instead of null so the Array.prototype.map() works on the initial render before the data is received.

The map() of the empty array won't produce anything in the rendered view

const [daysData, setDaysData] = useState([]);
about 4 years ago · Santiago Trujillo Report

0

Your array is null on rendering, and your api fetches after rendering your web app so you need to set it conditionally like this

{daysData !== null? daysData.map((day) => <div>{day.id}</div>:null)}
about 4 years ago · Santiago Trujillo 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!