Trying to create a switch to filter through on-site/remote job posts using UseEffect but it creates an infinite loop and I can't figure out why. (jobCondition, in this case, is UseState hook) + when I try to pass params in conditions: ${condition}, it does not work. Can somebody help explain to me what am I doing wrong in these 2 cases? Thanks!
const getJobsLocation = async () => {
const queryParams = { page: 1, limit: 10 };
const response = await getJobs(queryParams);
if (!response) {
return;
}
const searchResults = [];
response.data.nodes.forEach((loc) => {
searchResults.push({ title: loc.jobLocation });
});
getJobsLocation(searchResults);
console.log(searchResults);
};
useEffect(() => {
let condition = getJobsLocation();
switch (jobCondition) {
case 'On-site': condition.filter((con) => con.jobLocation === 'in_person');
break;
case 'Remote': condition.filter((con) => con.jobLocation === 'remote');
break;
case 'Hybrid': condition.filter((con) => con.jobLocation === 'hybrid');
break;
default:
condition = null;
}[condition];
if (condition != null) {
const params = new URLSearchParams({
jobs: jobFilter || null,
location: locationFilter || null,
since: datePosted || null,
conditions: `${condition}`,
});
history.push({ pathname: '/', search: `${params.toString()}` });
return;
}
const params = new URLSearchParams({
jobs: jobFilter || null,
location: locationFilter || null,
since: null,
conditions: null,
});
history.push({ pathname: '/', search: `${params.toString()}` });
}, [jobCondition]);
please note that let condition = getJobsLocation(); this function is an async function that needs to be await'ed on.. and it actually returns nothing so even after you await it it will be the condition variable will be undefined