skip to Main Content

Using the following code, I can see my data in the console but nothing is showing on the screen and there are no errors in the console. I assume there’s a problem in my return with the mapping but no idea what it is.

import { React, useState, useEffect } from "react";
import "./App.css";
import Card from "./Card";

function App() {
  const [data, setData] = useState([])

  let url = 'https://moviesdatabase.p.rapidapi.com/titles';


  function fetchData() { fetch(url, { method: 'Get', headers: {
    'X-RapidAPI-Key': redacted,
    'X-RapidAPI-Host': 'moviesdatabase.p.rapidapi.com'
  }})
    .then((res) => res.json())
    .then((json) => {
      console.log(json);
      setData(json);
      
    })};
  
    useEffect(() => {
      fetchData();
    }, [])

  return (
    <>
    My API <br/>
    <br />

    <div>
        {data.length && data.results.map((item) => {
          <div key={item.id}>
                <Card
                image={item.primaryImage.url}
                title={item.titleText}
                />
                </div>

})}
    </div>
    </>
  )
}

export default App

the card component code if needed:

import React from "react";

function Card(props) {
    return (
        <>
        <img src={props.image} alt={props.title} />
        <p>{props.title}</p>
        </>
    )
}

export default Card

2

Answers


  1. It seems like you have a small issue in your mapping function within the App component. The map function should return JSX, but you’re not returning anything from the arrow function inside map. Therefore, nothing is being rendered to the screen.

    Here’s the corrected code for the mapping function:

    {data.length && data.results.map((item) => (
      <div key={item.id}>
        <Card
          image={item.primaryImage.url}
          title={item.titleText}
        />
      </div>
    ))}

    By using parentheses () instead of curly braces {}, you’re implicitly returning the JSX inside the arrow function, and it should render the Card components properly.

    Login or Signup to reply.
  2. You missed the "return" inside map

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search