Last active
March 4, 2022 22:20
-
-
Save ozcanzaferayan/11ac6f8d4257094d99482cfcf1d81416 to your computer and use it in GitHub Desktop.
Testing React component - App.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 User = { | |
name: string; | |
followers: string; | |
following: string; | |
}; | |
function App() { | |
const [user, setUser] = useState<User | null>(null); | |
const [userName, setUserName] = useState<string>("octocat"); | |
const getUser = () => { | |
fetch(`https://api.github.com/users/${userName}`) | |
.then((response) => response.json()) | |
.then((data) => { | |
console.log(data); | |
setUser(data); | |
}); | |
}; | |
return ( | |
<div> | |
<input | |
type="text" | |
value="octocat" | |
onChange={(e) => setUserName(e.target.value)} | |
/> | |
<button onClick={() => getUser()}>Get user</button> | |
{user && ( | |
<> | |
<h1>{user.name}</h1> | |
<span>Followers: {user.followers}</span> | |
<br /> | |
<span>Following: {user.following}</span> | |
</> | |
)} | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment