I have recently started learning react an I am trying to create an application which makes use of an API which I created using Express.
Currently I am hosting my Express API locally at http://localhost:3000/api
However, when i open postman or my browser and send a GET request to http://localhost:3000/api/tasks I get all the tasks that are on my API, as expected.
My react application looks like this
class App extends React.Component{
constructor(){
super()
this.state = {
tasks: {}
}
}
...
componentDidMount() {
this.setState({loading: true})
fetch("http://localhost:3000/api/tasks/1")
.then(response => response.json())
.then(data => {
this.setState({
tasks: data
})
})
}
...
render() {
return(
<div>
<p>{this.state.tasks.id}</p>
</div>
}
}
When I open the dev tab in my browser I can see that the request (code: 304) is sent and there is a response which is a json file with the object I need. Nevertheless, I am unable to display the data I fetch from the API.