Created
March 8, 2024 04:18
-
-
Save Lahirutech/c80605262ad14694cb6edc6608152ea6 to your computer and use it in GitHub Desktop.
Fetch using fetch api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from 'react' | |
import './App.css' | |
export default function App() { | |
const FetachData = async (uri: string): Promise<any[]> => { | |
try { | |
const resp = await fetch(uri) | |
if (!resp.ok) { | |
throw new Error(resp.statusText) | |
} | |
console.log("resp",resp) | |
const dta = await resp.json() | |
return dta | |
} catch (error) { | |
console.log(error) | |
return [] | |
} | |
} | |
useEffect(() => { | |
FetachData("https://jsonplaceholder.typicode.com/posts").then(data => { | |
console.log(data) | |
}) | |
}, []) | |
return ( | |
<main> | |
React ⚛️ + Vite ⚡ + Replit 🌀 | |
</main> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment