Skip to content

Instantly share code, notes, and snippets.

@eldyvoon
Created June 29, 2019 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eldyvoon/f3113cb3c33cae23d2030508a45bbbe5 to your computer and use it in GitHub Desktop.
Save eldyvoon/f3113cb3c33cae23d2030508a45bbbe5 to your computer and use it in GitHub Desktop.
Users.js
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