fetch with useeffect hook

Solutions on MaxInterview for fetch with useeffect hook by the best coders in the world

showing results for - "fetch with useeffect hook"
Éloïse
01 Jul 2016
1import React, { useEffect, useState } from 'react';
2
3const User = () => {
4  const [users, setUsers] = useState([]);
5
6  useEffect(() => {
7    getUsers();
8  }, []);
9
10  async function getUsers() {
11    const response = await fetch('https://jsonplaceholder.typicode.com/users');
12    const users = await response.json();
13    setUsers(users);
14  }
15
16  return (
17    <ul>
18      {users.map(user => (
19        <li>{user.name}</li>
20      ))}
21    </ul>
22  );
23};
24
25export default User;
26