How to call an API in react (using useEfffect())

 Card.jsx :

import { useEffect, useState } from "react"

const Card1 = () => {

    const [allpost, setPost= useState([])

    const getUser = async () => {
        const response = await fetch('https://api.github.com/users')
        setPost(await response.json())
    }
    console.log(allpost);
    useEffect(() => {
        getUser()
    }, [])

    return (
        <>
            {
                allpost.map((post=> {
                    console.log(post)

                    return (
                        <>
                            <h1>User id : {post.id}</h1>
                            <h3>Login id: {post.login}</h3>
                            <p>Git URL : {post.url}</p>
                            <hr/>
                        </>
                    )

                })
            }
        </>
    )
}
export default Card1



Apps.jsx


import './App.css';
import Card1 from './Netflix/Card'

function App() {
  
  return (
    <div className="App">
    <Card1 />
    </div>
  );
}

export default App;



0 Comments