Skip to content

Instantly share code, notes, and snippets.

@Lahirutech
Created March 8, 2024 04:18
Show Gist options
  • Save Lahirutech/c80605262ad14694cb6edc6608152ea6 to your computer and use it in GitHub Desktop.
Save Lahirutech/c80605262ad14694cb6edc6608152ea6 to your computer and use it in GitHub Desktop.
Fetch using fetch api
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