I'm creating a simple GET API that collects data and spits it out as basic text in my front end. The API works and displays the data in console, but nothing is returned for the front end. Please help:
import React, { Component } from 'react';
import './Cards.css'
const data = {
name: 'test',
hobby: 'test'
}
function getUsers (data) {
fetch('http://localhost:3001/profile', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
})
.then((response) => response.json())
.then((data) => {
console.log('Success:', data)
})
.then((data) => {
return data.name
})
.catch((error) => {
console.error('Error:', error)
});
};
function Cards (data) {
return (
<div id='cards'>
<div className="card-header" id='card-header'>
Header
</div>
<div className="card-body" id='card-body'>
<blockquote className="blockquote mb-0">
<p>
{getUsers(data)}
</p>
<footer className="blockquote-footer">Someone famous in <cite title="Source Title">Source Title</cite></footer>
</blockquote>
</div>
</div>
);
}
export default Cards;
getUsersisasync, so you will need to use state for updating. Although things like this should be easier when suspense is live -> reactjs.org/docs/concurrent-mode-suspense.html.thenyouconsole.logthe data but don't return it, so it'll be lost and won't be passed to the next.then