Im trying to get json data from an api using react and insert it into an html page. Here's the function:
function GetApiContent() {
let value = fetch(api2)
.then(response => response.json())
.then(results => {
console.log(results.title)
return {
title: results.title,
date: results.date,
hdurl: results.hdurl,
explanation: results.explanation,
}
})
.catch(err => {
console.log(err)
})
return (
<div>
<h3>{value.title}</h3>
</div>
)
}
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Api test</h1>
<Hola message="hello" />
</header>
<GetApiContent />
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
)
}
}
Console log works and returns title but when i return it in the function it shows the h3 tag empty in the web page.
thenhandler.