I have 2 state variables, at the page load the program checks to see if there is an available agent and if yes, setDefault state variable is set to true, and then if a language change event is fired and there's an available agent the setStudio is set true, however when the conditional rendering is done default is always true, even though I set it to false in the eventListener fires, can anyone see what I'm doing wrong.
any help would be appreciated.
const Home = ({ t, i18n, history }) => {
const [defaultStudio, setDefaultStudio] = useState(false);
const [studio, setStudio] = useState(false);
useEffect(() => {
checkForAvailableAgent(LINK_TO_AGENT)
.then(res => {
setDefaultStudio(res);
})
.catch(error => {
console.log("an error happened.");
});
window.addEventListener("langChange", () => {
if(i18n.language === "en") {
checkForAvailableAgent(LINK_TO_AGENT)
.then(res => {
setStudio(res);
setDefaultStudio(false);
})
.catch(error => {
console.log("an error happened.");
});
} else {
setStudio(false);
setDefaultStudio(false);
}
});
},[defaultStudio, studio, i18n.language]);
return (
defaultStudio ?
(<Button>Call live</Button>)
:
(<Button>Call</Button>)
)
}