mirror of
https://github.com/lainbo/component-party.git
synced 2026-04-05 13:09:03 +08:00
30 lines
565 B
JavaScript
30 lines
565 B
JavaScript
export default function useFetchUsers() {
|
|
const users = $state();
|
|
const error = $state();
|
|
const isLoading = $state(false);
|
|
|
|
async function fetchData() {
|
|
isLoading = true;
|
|
try {
|
|
const response = await fetch("https://randomuser.me/api/?results=3");
|
|
users = (await response.json()).results;
|
|
} catch (err) {
|
|
error = err;
|
|
}
|
|
isLoading = false;
|
|
}
|
|
fetchData();
|
|
|
|
return {
|
|
get isLoading() {
|
|
return isLoading;
|
|
},
|
|
get error() {
|
|
return error;
|
|
},
|
|
get users() {
|
|
return users;
|
|
},
|
|
};
|
|
}
|