Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem in rendering API file using map and list in react

I am not able to render movies API(data file) in react when I am trying to do so using map . I have passed data in MovieCard Component.

No error is reported.

I even checked console.log(i) and its prints all the data of movies in the console perfectly but is not rendering it. Please Help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Warning in console: Line 18:13: Array.prototype.map() expects a return value from arrow function array-callback-return

//Line 18:13
<div className="list">    
 {data.map(i=>{

<MovieCard movie={i}/>


 })}

//Also replaced data.map with data.forEach but still no progress.


//App component

import React from "react";
import{data} from '../data'
import Navbar from './Navbar'
import MovieCard from './MovieCard'

function App() {
  return (
    <div className="App">
 <Navbar/>
 <div className="main">

<div className="tabs">
<div className="tab">Movies</div>
<div className="tab">Favourites</div>
</div>

<div className="list">    
 {data.map(i=>{
  
<MovieCard movie={i}/>


 })}

</div>

</div>
</div>

);
}

export default App;



//MoviCard Component
import React, { Component } from 'react'


export class MovieCard extends Component 
{
    render() {

        const {movie} = this.props;
        return (
            <div className="movie-card">
            <div className="left">

                <img alt="movie-poster" src={movie.Poster}/>
            </div>
    
          <div className="right">
           <div className = "title"> {movie.Title} ({movie.Year})</div>
           <div className = "plot">{movie.Plot}</div>
           <div className = "footer">
           <div className = "rating">{movie.imdbRating}</div>
           <button className="favorite-btn">Favourite</button>
          </div>
            </div>

            </div>
        )
    }
}

export default MovieCard;



>Solution :

The problem is exactly what you reported:

Array.prototype.map() expects a return value from arrow function

The function you’re passing to .map doesn’t return anything:

data.map(i=>{
  <MovieCard movie={i}/>
})

You can explicitly return:

data.map(i=>{
  return <MovieCard movie={i}/>
})

Or simplify by not wrapping the function block in curly braces, but instead just use parentheses and rely on the implicit return for single-line arrow functions:

data.map(i=>(
  <MovieCard movie={i}/>
))

As an aside, in React you’ll also want rendered lists like this to have a key attribute. If i has a unique ID then you can use that. For example:

data.map(i=>(
  <MovieCard key={i.id} movie={i}/>
))

Otherwise you can rely on the second argument passed to the .map callback function, which is just an index:

data.map((i,x)=>(
  <MovieCard key={x} movie={i}/>
))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading