Skip to content

Instantly share code, notes, and snippets.

@rkpontes
Last active November 8, 2024 13:43
Show Gist options
  • Save rkpontes/1820c954261b62aba73828cbabaf3186 to your computer and use it in GitHub Desktop.
Save rkpontes/1820c954261b62aba73828cbabaf3186 to your computer and use it in GitHub Desktop.
React useState and useEffects
import React, {useEffect, useState} from 'react';
import './style.css';
// https://63f8ba616978b1f9105fb041.mockapi.io/users
function App() {
const [users, setUsers] = useState([]);
useEffect(() => {
function loadApi() {
let url = 'https://63f8ba616978b1f9105fb041.mockapi.io/users';
fetch(url)
.then((r) => r.json())
.then((json) => {
console.log(json);
setUsers(json);
});
}
loadApi();
}, []);
return (
<div className='container'>
<header>
<strong>Lista de Usuários</strong>
</header>
{users.map((user) => {
return (
<article key={user.id} className='user'>
<strong className='name'>{user.name}</strong>
<span className='email'>{user.email}</span>
<span className='phone'>{user.phone}</span>
<a href='#' className='button'>Enviar email</a>
</article>
)
})}
</div>
);
}
export default App;
body {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Arial, sans-serif;
}
.container {
display: flex;
flex-direction: column;
width: 100%;
justify-content: center;
align-items: center;
}
header {
display: flex;
justify-content: center;
align-items: center;
font-size: 35px;
height: 60px;
width: 100%;
background-color: #4c89e3;
color: white;
}
.user {
display: flex;
flex-direction: column;
justify-content: center;
width: 700px;
background-color: white;
margin: 20px;
}
.name {
font-size: 20px;
margin-bottom: 10px;
}
.email, .phone {
font-size: 15px;
margin-bottom: 10px;
color: #4c89e3;
}
.button {
height: 30px;
background: none;
border-radius: 5px;
color: #4c89e3;
border: 2px solid #4c89e3;
display: flex;
justify-content: center;
align-items: center;
font-size: 15px;
font-weight: bold;
transition: all 0.5s;
}
.button:hover {
background-color: #4c89e3;
color: white;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment