Using async await
useEffect(() => {
const fetchUsers = async () => {
try {
const res = await axios
.get<User[]>("https://jsonplaceholder.typicode.com/xusers")
setUsers(res.data);
} catch (error) {
setError((error as AxiosError).message)
}
}
fetchUsers();
}, []);