I'm trying to add a whole array to the useState variable but I keep getting null, what the program is supposed to do is that first it checks to see if a default studio is available and add that to defaultStudioAvailable and then take that studio out from the studios array and then does another check on all the other studios and adds them to the others array with the line others[studio] = res; and adds that to state variable otherStudiosAvailable, however I get the correct details in the others array but nothing gets added to the otherStudiosAvailable state array,
can anyone see what I'm doing wrong? any help would be appreciated.
const Home = ({i18n}) => {
const [defaultStudioAvailable, setDefaultStudioAvailable] = useState();
const [otherStudiosAvailable, setOtherStudiosAvailable] = useState([]);
const studios = ["de", "fr", "pl", "en", "nl", "ru"];
const otherStudios = studios.filter(item => {
return item !== i18n.language;
});
useEffect(() => {
let others = [];
checkForAvailableAgent(
`sales_${i18n.language}`,
"LinkToStudio",
"ServiceID"
)
.then(res => {
setDefaultStudioAvailable(res);
})
.catch(error => {
console.log("an error happened.");
});
for (const studio of otherStudios) {
checkForAvailableAgent(
`sales_${studio}`,
"LinkToStudio",
"ServiceID"
)
.then(res => {
// this has the correct values
others[studio] = res;
// this keeps being null even though others have values
setOtherStudiosAvailable(others);
})
.catch(error => {
console.log("an error happened.");
});
}
console.log("others[studio]: ", others);
console.log("other studios available: ", otherStudiosAvailable);
}, [defaultStudioAvailable, i18n.language]);
}
// console output
others[studio]: [en: false, fr: false, nl: false, pl: false, ru: false]
other studios available: []
{defaultStudioAvailable ? (
<Button
variant="success"
className="btn-xlarge"
onClick={e => callBtnClick("direct")}
>
{t("callBtn")}
</Button>
) : otherStudiosAvailable ? (
<Button
variant="success"
className="btn-xlarge"
onClick={e => callBtnClick("flag")}
>
Other studios available
</Button>
) : (
""
)}