interface ProfInfo {
Repos: number;
Followers: number;
Following: number;
}
export default function Profile() {
const [user, setUser] = useState<string>("");
const [profInfo, setProfInfo] = useState<ProfInfo>({
Repos: 0,
Followers: 0,
Following: 0,
});
useEffect(() => {
const fetchData = async () => {
try {
const response = await fetch(`https://api.github.com/users/${user}`);
const data = await response.json();
setProfInfo({
Repos: data.public_repos,
Followers: data.followers,
Following: data.following,
});
console.log(data);
} catch (error) {
console.error("Error fetching data:", error);
}
};
fetchData();
}, [user]);
const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
setUser(event.target.value);
};
return (
<>
<main>
<div className="profile">
<input type="text" value={user} onChange={handleInputChange} />
<div>
<h1></h1>
<p>Repos: {profInfo.Repos}</p>
<p>Followers: {profInfo.Followers}</p>
<p>Following: {profInfo.Following}</p>
</div>
</div>
</main>
</>
);
}
#Api help
61 messages · Page 1 of 1 (latest)
ok hey
so what im little confsed is if i want to make all this stuff
nickname |
user |
location link
i wil have to make new types in the profInfo?
and the user is just the input im entering to find the data in api
?
can you understand what im asking>?
well no not trynabe rude just wanna get it in practice and
can you explain why this
i got [user] isnt dependecy array to make useEffect load on
when page is loaded once
[user] is when the input changes
so i wil be getting new info from api everytime user changes?
yes
interface ProfInfo {
Repos: number;
Followers: number;
Following: number;
}
i got this right im doing the setProfInfo({
repos : data.public_repos)}
so i can just do profinfo.repos?
interface is for setting a class for typescript nothing more
yes ik
you should read the document or watch some guides
repo: data.public thing
data.public is the data from the api
yes i get that from api bt that
i get hold of it by that
then i updated it and send it to profInfo
so i can use profInfo.followers
yes
okay think i understand everything thansk keder
no problem
just saw useEffect for first time
yeah read the document first
i was stuck on useState
idk where to loock
when i started react now that i grasped that its seems easy
and new methods
thank you
no problem
dumb question