Answers for "useEffect hook while fetching user"

1

fetch with useEffect hook

import React, { useEffect, useState } from 'react';

const User = () => {
  const [users, setUsers] = useState([]);

  useEffect(() => {
    getUsers();
  }, []);

  async function getUsers() {
    const response = await fetch('https://jsonplaceholder.typicode.com/users');
    const users = await response.json();
    setUsers(users);
  }

  return (
    <ul>
      {users.map(user => (
        <li>{user.name}</li>
      ))}
    </ul>
  );
};

export default User;
Posted by: Guest on August-25-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language