console.log(`fetching ${url}...`)
}, [url])
Is something like that ok?
You do it like this: let url = 'https://google.com' useEffect(() => { console.log(`fetching ${url}...`) }, [])
the difference is, it re-renders every time the url changes but when given empty brackets, it only renders once before the components are loaded
You can also use the below model (though there are efficient & better ways using SWR, React Query, etc..) import { useEffect, useState } from "react"; import { getUserDetails } from "../api/services"; export default function useUser() { const [loading, setLoading] = useState<boolean>(true); const [user, setUser] = useState<any>({}); useEffect(() => { setLoading(true); getUserDetails().then(data => { setUser(data.data.data); setLoading(false); }).catch(error => { console.log(error) }); }, []); return { user, loading }; }
yea and most of the time I don't need it to fech again every redner
data.data.data 🤯
haha :D
Обсуждают сегодня