I am creating a simple movie website using React, the details such as name, genre, description e.t.c all are displayed except for the movie image which is provides in the movie_data.js file. How do I solve this and what could be the issue?
movie_data.js file:
export default [
{
id: 1,
name: "Oppenheimer",
image:"../images/opennheimer.jpg",
description: "The story of J. Robert Oppenheimer’s role in the development of the atomic bomb during World War II.",
Genre: "Biography, Drama, History",
Actor: "Cillian Murphy, Emily Blunt, Robert Downey Jr",
Director: "Christopher Nolan",
Country: "United States, United Kingdom"
}
]
movie_component.js file:
function MovieComponent(prop)
{
return (
<div className="movie-container">
<img src={prop.items.image} alt={prop.items.name}/>
<h2>{prop.items.name}</h2>
<p>{prop.items.description}</p>
<p>{prop.items.Genre}</p>
<p>{prop.items.Actor}</p>
<p>{prop.items.Director}</p>
<p>{prop.items.Country}</p>
</div>
)
}
movie_app.js file:
function MovieApp()
{
const movieElements = movie_data.map(function(dataItems){
return <MovieComponent id={dataItems.id} items={dataItems}/>
})
return (movieElements)
}
I expected the images for each movie to be displayed, however I only get text details.
2
Answers
You can solve this issue using ES5 require property like below. but make sure to set the correct path of image relative to your component directory.
I think your path is not resolving correctly. You can get this solved in two ways:
I’m sure both of these solutions would work for your use-case.