useEffect fetching
Answered
Siamese posted this in #help-forum
SiameseOP
the loading state is set to false before other data. without the controller it works fine
useEffect(() => {
const controller = new AbortController();
axios.get(`/api/db/custombot?guild=${guildID}`, {signal: controller.signal})
.then(function (response: any) {
setAvatar(response?.data.avatar);
setName(response?.data.name);
setActivityValue(response?.data.activity);
setActivityType(activityOptions.find((x) => x.value === response?.data.activityType));
setStatusType(options.find((x) => x.value === response?.data.status));
}).catch(error => {
if (error.response?.status === 401) router.push(error.response?.data.redirect);
}).finally(() => setLoading(false));
return () => controller.abort();
}, [guildID]);Answered by Siamese
final code
useEffect(() => {
const controller = new AbortController();
axios.get(`/api/db/custombot?guild=${guildID}`, {signal: controller.signal})
.then(function (response: any) {
if (response) {
setAvatar(response.data.avatar);
setName(response.data.name);
setActivityValue(response.data.activity);
setActivityType(activityOptions.find((x) => x.value === response.data.activityType));
setStatusType(options.find((x) => x.value === response.data.status));
}
}).catch(error => {
if (error.response?.status === 401) router.push(error.response?.data.redirect);
}).finally(() => {
if (!controller.signal.aborted) setLoading(false);
});
return () => controller.abort();
}, [guildID]);14 Replies
Common paper wasp
you sure you don't have any errors in catch block?
When I remove the abort controller the issue I got goes away
SiameseOP
.
@Siamese When I remove the abort controller the issue I got goes away
try this
useEffect(() => {
setLoading(true)
const controller = new AbortController();
axios.get(`/api/db/custombot?guild=${guildID}`, {signal: controller.signal})
.then(function (response: any) {
setAvatar(response?.data.avatar);
setName(response?.data.name);
setActivityValue(response?.data.activity);
setActivityType(activityOptions.find((x) => x.value === response?.data.activityType));
setStatusType(options.find((x) => x.value === response?.data.status));
}).catch(error => {
if (error.response?.status === 401) router.push(error.response?.data.redirect);
}).finally(() => setLoading(false));
return () => controller.abort();
}, [guildID]);SiameseOP
So you set the set loading to true, right? No more changes?
Because I made the loading state be true by default
yes
SiameseOP
Alright. I will test it soon
@Ray yes
SiameseOP
Didn't help
if (response) setLoading(false);
this solved
so it set loading to false even for the aborted request
SiameseOP
final code
useEffect(() => {
const controller = new AbortController();
axios.get(`/api/db/custombot?guild=${guildID}`, {signal: controller.signal})
.then(function (response: any) {
if (response) {
setAvatar(response.data.avatar);
setName(response.data.name);
setActivityValue(response.data.activity);
setActivityType(activityOptions.find((x) => x.value === response.data.activityType));
setStatusType(options.find((x) => x.value === response.data.status));
}
}).catch(error => {
if (error.response?.status === 401) router.push(error.response?.data.redirect);
}).finally(() => {
if (!controller.signal.aborted) setLoading(false);
});
return () => controller.abort();
}, [guildID]);Answer