This is my newsapp and this my News.js file of component folder
import React, { Component } from 'react'
import NeswItem from './NewsItem'
export class News extends Component {
constructor(){
super();
this.state={
data : null,
loading : false
}
}
componentDidMount(){
let url = //your news api url
fetch(url).then((res)=>{
res.json().then((result)=>{
console.log(result.articles)
this.setState({data:result.articles})
})
})
}
render() {
return (
<>
<div className="container my-3">
<h2>Top Headlines</h2>
<div className='row'>
{this.state.data ?
this.state.data.map((element)=>
<div className="col-md-4" key={element.url} >
<NeswItem title={element.title?.slice(0, 45)} description={element.description?.slice(0, 88)} imgurl={element.urlToImage} newsUrl={element.url}/>
</div>
)
: null
}
</div>
</div>
</>
)
}
}
export default News
I am creating a react app to show latest news and this is my App.js file
import './App.css';
import React, { Component } from 'react'
import Navbar from './components/Navbar';
import News from './components/News';
export default class App extends Component {
render() {
return (
<>
<News/>
</>
)
}
}
and this is my NewsItem.js file from component folder
import React, { Component } from 'react'
export class NeswItem extends Component {
render() {
let {title, description, imgurl,newsUrl}= this.props;
return (
<div>
<div className="card my-3" style={{width: "18rem"}}>
<img src={!imgurl?"https://img.etimg.com/thumb/msid-96022092,width-1070,height-580,imgsize-60058,overlay-economictimes/photo.jpg":imgurl} className="card-img-top" alt="..."/>
<div className="card-body">
<h5 className="card-title">{title}...</h5>
<p className="card-text">{description}... </p>
<a href={newsUrl} target="_blanck" className="btn btn-primary">Read more</a>
</div>
</div>
</div>
)
}
}
export default NeswItem
I want to convert my componentDidMount function into async componentDidMount one but I was unable to do it. note: I am using my api from newsapi.org