I am doing a multi filter component, which can filter by keyword, category work types and so on... so this is one useEffect Hook, this filters by entering keyword of job titles and job category.
useEffect(() => {
const newFilteredJobs = jobs.filter((job) => {
return (
job.title.toString().toLocaleLowerCase().includes(searchField) &&
job.category
.toString()
.toLocaleLowerCase()
.includes(categorySearchField)
);
});
setFilteredJobs(newFilteredJobs);
}, [jobs, searchField, categorySearchField]);
I would like to filter one more which is work type, however, this requires pass props from child component to parent component.
This is in the parent component
<WorkType jobs={jobs} onFilterJobsByType={filterJobsByTypeHandler} />
This is my filterJobsByTypeHandler
function
const filterJobsByTypeHandler = (jobTypes) => {
console.log(`selected job types are ${jobTypes}`);
const newFilteredJobs = jobs.filter((job) => {
for (let i = 0; i < jobTypes.length; i++) {
if (job.jobType !== undefined) {
return job.jobType.toString() === jobTypes[i];
}
}
});
setFilteredJobs(newFilteredJobs);
};
I need to select and deselect job types to refresh the filtered jobs, I suppose I need to use useEffect hook? As I can control the dependencies. But i am struggling how I can put the function inside of the useEffect hook.
I tried to use useCallBack Hook, but I could not get the full list of jobs by deselecting the selected work types.