I am trying to setState of a boolean isLoading variable and using that to show a css loader on top of my component. So what I did was :
when I click on a button I setState the isLoading variable from false to true and execute my async function.
After the async finishes I just set the isLoading to false using setState()
my problem is that when I first set the isLoading variable to true it doesn't get set and the component does reRender without changing the state ( I even console log the state of that variable on the render function )
I did use the callback function as demonstrated below and still no use.
my code :
async sendEmail({to,from,subject,html,text}){
this.setState({
isEmailLoading:true
},()=>{
emailRequests.SendAnEmail({
to:to,
text:text,
subject:subject,
html:html,
from:from
}).then((data)=>{
this.setState({
isEmailLoading:false,
});
toaster.success({
title:"Email sent",
text:"Your email was sent to "+to,
});
console.log(data);
}).catch((error)=>{
this.setState({
isEmailLoading:false,
});
console.error(error);
toaster.error({
title:"Email not sent",
text:"check console for more information",
});
})
})
}
my state :
this.state={
email:{
to:"",
subject:"",
from:"",
html:"",
text:""
},
isEmailLoading:false,
isEmailError:false,
isEmailSuccess:false,
rawtext:""
}
Please help this is absolutely not normal.
FIX :
This is very weird and I couldn't debug it, but it turned out that I am using a third party button component that when clicked on, doesn't change state no matter what happens.
Just switching that component to an other button component worked perfectly.