Hi everyone!
I have decided to use React and Contentful for a small project of mine. I have created four articles which have title, date, image and full article. I can get and display on page title, date and full article(text). But no way, no matter what I do, I cant display image.
I have tried this porject just with JS & HTML and I could display all items.
I really dont get it, why the same path to the image url works in JS but not in React.
Code in my Articles.js
import React, { useContext } from “react”;
import Article from “./components/Article”;
import { Context } from “./Context”;
function Articles() {
const { allArticles } = useContext(Context);
console.log(allArticles);
const articleElement = allArticles.map((news) => (
<Article key={news.fields.id} news={news} />
));
return
}
export default Articles;
And this is my Article.js
import React from “react”;
function Article({ news }) {
const { title, date, besedilo } = news.fields;
const image = news.fields.image.fields.file.url;
return (
<div className="show-news">
{/* <img src={news.fields.image.fields.file.url} alt="article" /> */}
<img src={news.image} alt="article" />
<div className="news-content">
<h2>{title}</h2>
<p>{date}</p>
<p>{besedilo}</p>
</div>
</div>
);
}
export default Article;
And the ERROR
TypeError: news.fields.image.fields is undefined
Article
C:/Users/bostj/Desktop/get-news/src/components/Article.js:5
2 |
3 | function Article({ news }) {
4 | const { title, date, besedilo } = news.fields;>
5 | const image = news.fields.image.fields.file.url;
6 | return (
7 | <div className="show-news">
8 | {/* <img src={news.fields.image.fields.file.url} alt="article" /> */}