Resolving Uncaught TypeError in React when trying to read properties of undefined (specifically 'map')

As a newcomer to React and API usage, I am facing difficulty resolving the error below. My goal is to showcase a carousel of images using React.js but encountered this issue:

Error message: Popular.jsx:26 Uncaught TypeError: Cannot read properties of undefined (reading 'map')

In addition to the above error, another error message appears:

The provided error originated in the following component: Popular (http://localhost:3001/static/js/bundle.js:120:80) within div under Home within div under Pages within div under App Consider incorporating an error boundary into your codebase for customized error handling. Go to https://reactjs.org/link/error-boundaries to gain insights on error boundaries.

I would appreciate any guidance on how to resolve this issue as my attempts via YouTube and Google have not led to a solution.

Here's a snippet of my code:

import { useEffect, useState } from 'react';
import styled from 'styled-components';
import { Splide, SplideSlide } from '@splidejs/react-splide';
import '@splidejs/splide/dist/css/splide.min.css';

function Popular() {
  const [popular, setPopular] = useState([]);

  useEffect(() => {
    getPopular();
  }, []);
  console.log(process.env.REACT_APP_API_KEY);
  const getPopular = async () => {
    const api = await fetch(
      `https://api.spoonacular.com/recipes/random?apiKey=${process.env.REACT_APP_API_KEY}&number=9`
    );
    const data = await api.json();

    setPopular(data.recipes);
  };

  return (
    <div>
      <Wrapper>
        <h3>Popular Picks</h3>
        <Splide
          options={{
            perPage: 4,
            arrows: false,
            pagination: false,
            drag: 'free',
            gap: '5rem'
          }}
        >
          {popular.map((recipe) => {
            return (
              <SplideSlide>
                <Card>
                  <p>{recipe.title}</p>
                  <img src={recipe.image} alt={recipe.title} />
                </Card>
              </SplideSlide>
            );
          })}
        </Splide>
      </Wrapper>
    </div>
  );
}

const Wrapper = styled.div`
  margin: 4rem 0rem;
`;

const Card = styled.div`
min-height: 25rem;
border-radius: 2rem;
overFlow: hidden;
position: relative;

img{
border-radius: 2rem;
position: absoute;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
}

p{
  position: absolute;
  z-index: 10;
  left: 50%;
  bottom: 0%;
  transform: translate(-50%, 0%);
  color: white;
  width: 100%;
  text-align: center;
  font-weight: 600;
  font-size: 1rem;
  height: 40%;
  display: flex;
  justify-content: center;
  align-items: center;

}

`;

export default Popular;

Your assistance is highly appreciated!

Answer №1

By initializing your state with an empty array, you are ensuring that .map will not throw any errors.

If you suspect an issue with your API key/call/response, try logging the response after this line: const data = await api.json();.

For example, without a key, calling the API results in the following response:

  { 
    status: "failure", 
    code: 401, message: 
    "You are not authorized. Please read https://spoonacular.com/food-api/docs#Authentication" 
  }

If there is an error and you call setPopular(data.recipes);, it is equivalent to saying setPopular(undefined). Subsequently, undefined.map(...) will trigger an error.

To avoid such errors, consider checking whether popular is truthy.

In the render method, modify {popular.map((recipe) => { to

{popular && popular.map((recipe) => {

If you want to ensure no errors occur, verify if popular is an array:

{Array.isArray(popular) && popular.map((recipe) => {

You can implement a similar approach to render a different component when there is no data available:

  if (popular) {
    <SplideSlide>...</SplideSlide>
  } else {
    <NoSlidesAvailableComponent />
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to refresh a Material-UI data table in React whenever a user takes any action?

I am facing an issue with my Lock-Unlock button and delete button. The problem arises when I render data from axios using the useEffect hook, it works fine. However, if I try to lock or unlock a user, the table does not update automatically. This indicates ...

How to link an external CSS file to a Vue.js project

I created a new project using @vue/cli and now I want to add an external CSS file to my App.vue Here's what I attempted: <template> <div id="app"> <div id="nav"> <router-link to="/">Home</router-link> | ...

Experiencing difficulties replicating two auto-scrolling divs

I have implemented a script to automatically slide two different divs. Here is the code I am using: The HTML: <div id="gallery"> <div id="slider" style="width: 6000px; left: -500px;"> <div><aside class="widget widget_testimoni ...

Trouble extracting and utilizing GraphQL queries in Typescript with relay-compiler

I attempted to utilize relay with the Typescript react starter, but I am encountering several problems. It appears that babel-plugin-relay is unable to detect the graphql statements extracted by the relay-compiler. Below is my compiler script: "relay": " ...

Tips for delivering a variable to a React Native Stylesheet

Is there a way to pass a variable to the "shadowColor" property in my stylesheet from an array declared in the code above? I keep encountering a "Can't find name" error. Attempting to use a template literal has not resolved the issue. Any assistance w ...

Sharing state between two functions in React using Hooks

Coming from a background in Vue, I am struggling to comprehend how to conditionally show something when the HTML is fragmented into different parts. Imagine having this structure: import React, { useState } from "react"; const [mobileNavOpen, setMobi ...

Heroku is eliminating non-essential dependencies that are not required for development

I've developed a Next.js app on my local machine which is working fine, but when I deploy it to Heroku, the build is successful. However, when I check the server.js file, I encounter an error. I tried to debug the error by using 'heroku logs --ta ...

The element 'fontFamily' is not recognized within the 'ThemeOptions' type in MUI theming

I'm diving into the world of React and MUI by building my own dashboard from scratch. Let's take a look at my App.tsx file: import React from 'react'; import ReactDOM from 'react-dom/client'; import './index.css'; i ...

Encasing words within a cell of a table

My goal is to ensure that the table width remains fixed and unaffected by long text strings. Even though I have PHP code in place to limit characters at 100 per cell, I want to guarantee that a lengthy text string will never expand the table beyond its set ...

HTML5 footer element is the perfect way to add a

I'm currently working on creating a footer that stretches across the entire width of the screen. Originally, I had it nested within the body tag which was centered with a width of 825px. To remove this width constraint, I moved the footer under the ht ...

React's Bootstrap Modal Failing to Close

Check out the following modal component. The issue seems to be with the code block where (isPending == "hide") triggers a console.log, but bsModal.hide() doesn't function as expected. If I remove the bsModal.show() line of code, the modal ...

Is there a method to view text options that are too long within a fixed width <select multiple> HTML box?

Here is the code I am working with: <div id='div2' style='height: 430px; width: 350px; overflow:auto;'> <select multiple id="id" size="28" style="FONT-SIZE: 12px; FONT-FAMILY: Arial; width: 100%"> It's a challenge bec ...

Styling for main banner image on large desktop screens and mobile devices

I need help with website design and CSS solutions. Currently, I have a WordPress website with a onepager theme. At the top of the page, there is an image that almost fills the screen along with a title. My concern is how this image behaves with different b ...

How to send an image file to Amazon S3 using React JS

I have successfully implemented code that can upload several files to an S3 bucket. However, there seems to be an issue with the uploaded image files as they appear corrupted and cannot be opened. I am looking for a way to upload these files as images or ...

Enhance the list visualization in Next.js by efficiently transferring data from child components

In my Next.js Page component, I have a setup similar to the following: export default function Index({ containers }) { const [containerListState, setContainerListState] = useState(containers); const updateContainerList = (container) => { contai ...

Stop the ability to navigate backwards using the useNavigate hook

Is it possible to disable back navigation in React by utilizing the useNavigate hook? I have been using navigate("/",{replace: true}) temporarily, but am searching for a more elegant solution. ...

Is there a way to modify the border shape of items in the navigation bar?

Is there a way to achieve this particular shape when the current page is being displayed? https://i.stack.imgur.com/HELTM.jpg This is how my current code looks: current { transform: skew(-20deg); border: 1px solid white; color: black; background ...

Steps for automatically playing the next song when a button is clicked

I have encountered a challenge in developing a music player. The issue lies in the loading of the next song when the user clicks the 'next' button. While the new data is successfully updated in both the state and render, the music does not automa ...

Leveraging IntersectionObserver to identify the video in view on the screen

Our Objective I aim to implement a swipe functionality for videos where the URL changes dynamically based on the ID of the currently displayed video. Challenges Faced Although I managed to achieve this with code, there is an issue where the screen flashe ...

Unable to produce audio from files

Attempting to incorporate sound files into my project using https://github.com/joshwcomeau/redux-sounds but encountering difficulties in getting it to function. Below is the code snippet I utilized for setup. Unsure if webpack is loading the files correctl ...