1
I have my header component, where I search the user information through the api, the problem is that this search gets stuck in a loop that keeps searching the user non-stop.
code::
import React, { useState, useMemo } from "react";
import axios from "axios";
import Logout from '../Logout/';
import "./style.css";
const user_id = localStorage.getItem('user_id');
const avatar = localStorage.getItem('avatar');
export default function Header(){
const [user, setUSer] = useState(null);
useMemo(async () => {
const data = await axios.get(`http://localhost:3001/api/users/${user_id}`);
setUSer(data.data)
console.log(data);
})
console.log(user)
if(user !== null){
return (
<header className="async-header">
<div className="user-content">
<span className="user-name">
{user.username}
<span>{user.email}</span>
<a href="/#" className="btn-logout" onClick={Logout}>Deslogar</a>
</span>
<div className="user-image" style={user.avatar ? {backgroundImage: `url(${user.avatar})`} : null}></div>
</div>
</header>
)
}else{
return <div />
}
}
Thank you for the answer and the explanation. I had tested an example, just like this one and it hadn’t worked, a few hours later I found out that React for some reason hadn’t updated the code and was rendering the site using a slightly worse example than I put on it. More after restarting the server started working again.
– Lukas Takahashi
Your explanation helped me create a cleaner code.
– Lukas Takahashi
No problem, happy to help
– Gabriel Brito