I'm trying to get a certain return value (of type string) from fetch() but I'm failing, allthough the data is there when I try to display it on console. The thing is that I need to do in a "function Component". That means it works in a "class Component" with setState. here is the code : too simple
import {useState} from 'react';
function ProfilePhoto(props) {
const [data,setData]=useState(()=>fetch(url)
.then((response)=>response.json())
.then((data)=>{
console.log(data);
return data.userPhoto;}));
return (
<div>
{"data: "+data}
<input id="picture" type="file" name="file" />
<img src={"./images/"+ data}></img>
</div>
);
}
export default ProfilePhoto;
Data in console:
{id: 1, username: 'Paul', userPhoto: 'photo1.jpg', posts: Array(4), login: null, …}