Answers for "fetch inside useeffect"

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
0

how to fetch data with useEffect

useEffect(() => {
    const fetchData = async () => {
      const result = await fetch(
        'https://my-api.com',
      );
 
      setState(result);
    };
 
    fetchData();
  }, []);
Posted by: Guest on April-10-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language