-
-
Save eldyvoon/f3113cb3c33cae23d2030508a45bbbe5 to your computer and use it in GitHub Desktop.
Users.js
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 React, { useState, useEffect } from "react"; | |
import axios from "axios"; | |
import styled from "styled-components"; | |
const Loader = styled.div``; | |
const Row = styled.li` | |
list-style: none; | |
margin: 10px; | |
`; | |
export const url = "https://randomuser.me/api/?results=5"; | |
function Users() { | |
const [users, setUsers] = useState([]); | |
useEffect(() => { | |
async function getUsers() { | |
try { | |
const { | |
data: { results } | |
} = await axios.get(url); | |
setUsers(results); | |
} catch (error) { | |
console.log(error); | |
} | |
} | |
getUsers(); | |
}, []); | |
return ( | |
<> | |
{users.length === 0 ? ( | |
<Loader>Loading...</Loader> | |
) : ( | |
users.map(user => ( | |
<Row key={user.name.first} data-testid="row"> | |
{user.name.first} | |
</Row> | |
)) | |
)} | |
</> | |
); | |
} | |
export default Users; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment