Created
March 4, 2022 22:58
-
-
Save ozcanzaferayan/06915335012fa1b0072663213c0d519f to your computer and use it in GitHub Desktop.
Testing react components - Repos.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from "react"; | |
type Repo = { | |
full_name: string; | |
}; | |
function Repos() { | |
const [repos, setRepos] = useState<Repo[] | null>(null); | |
const [userName, setUserName] = useState<string>("octocat"); | |
const getRepos = () => { | |
fetch(`https://api.github.com/users/${userName}/repos`) | |
.then((response) => response.json()) | |
.then((data) => { | |
console.log(data); | |
setRepos(data); | |
}); | |
}; | |
return ( | |
<div> | |
<input | |
type="text" | |
value="octocat" | |
onChange={(e) => setUserName(e.target.value)} | |
/> | |
<button onClick={() => getRepos()}>Get repos</button> | |
{repos && ( | |
<> | |
<h1>{`${userName} repos`}</h1> | |
{repos.map((repo) => ( | |
<span key={repo.full_name}>{repo.full_name}</span> | |
))} | |
</> | |
)} | |
</div> | |
); | |
} | |
export default Repos; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment