From 2b3f6052dde3448066e670d504873c50382405a9 Mon Sep 17 00:00:00 2001 From: Prayag Bhatia Date: Fri, 14 Oct 2022 22:48:39 +0530 Subject: [PATCH] ADD : news articles from NEWS api --- src/components/News.js | 33 +++++++++++++++++++++++++++------ src/components/NewsItem.js | 15 +++++++++++++-- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/components/News.js b/src/components/News.js index afaffc4..6255d9c 100644 --- a/src/components/News.js +++ b/src/components/News.js @@ -2,14 +2,35 @@ import React, { Component } from 'react' import NewsItem from './NewsItem' export class News extends Component { + + constructor() { + super(); + this.state = { + articles: [], + loading: false + } + } + + //Lifecycle method run after render method is called ; used for loading of data + async componentDidMount() { + let url = "https://newsapi.org/v2/everything?q=india&from=2022-09-14&sortBy=publishedAt&apiKey=7d248d9c8ea64fa6a4691432a30a74ac" + let data = await fetch(url) ; + let parsedData = await data.json() + this.setState({articles: parsedData.articles}) + } + render() { return ( -
This is a news component - - - - - +
+

NewsMonkey -- Top Headlines

+
+ {this.state.articles.map((element) => { + return
+ +
+ })} +
) } diff --git a/src/components/NewsItem.js b/src/components/NewsItem.js index 96b51f6..84c52a4 100644 --- a/src/components/NewsItem.js +++ b/src/components/NewsItem.js @@ -1,10 +1,21 @@ import React, { Component } from 'react' export class NewsItem extends Component { + render() { + let {title, description, imageUrl, newsUrl} = this.props ; + let defaultImage = 'https://www.google.com/imgres?imgurl=https%3A%2F%2Fimages.ctfassets.net%2Flh3zuq09vnm2%2F1ENjVxhiWtm7LCwLFhgiW9%2F3132b27acddbfab3131686ea80310fdc%2Fconducting-usability-test_yon4BQT.svg&imgrefurl=https%3A%2F%2Fwww.hotjar.com%2Fusability-testing%2Fprocess-examples%2F&tbnid=RfcanOTfNU2P-M&vet=12ahUKEwiM5J27-d_6AhVDi9gFHXu4BqUQMygCegUIARDeAQ..i&docid=ipEmDD0rz65fGM&w=800&h=534&q=test&hl=en&ved=2ahUKEwiM5J27-d_6AhVDi9gFHXu4BqUQMygCegUIARDeAQ' + return ( -
- This is a news item +
+
+ ... +
+
{title}
+

{description}

+ Read More +
+
) }