I am trying to create an app where the user can click on a category and it will set the displayWork
array to the value of the category and thus display the items in that array.
I have all of the work stored in an array
const [workList, setWorkList] = useState([
{ title: "aaa", image: "./images/1.jfif", type: "landscaping" },
{ title: "plumbing", image: "./images/2.jfif", type: "plumbing" },
{ title: "Other 3", image: "./images/3.png", type: "other3" },
{ title: "Other 4", image: "./images/3.png", type: "other4" },
]);
and then I have my displayWork
mapped to the page, I plan on changing it through using array.filter()
let filteredWork = workList.filter(function (work) {
return work.type === catagory;
});
then I have a row of categories like this to set the parameters of the filter
<div className="col-sm-2 workCatagory" onClick={() => setCatagory("landscaping")}>
Land Scaping
</div>
<div className="col-sm-2 workCatagory" onClick={() => setCatagory("plumbing")}>
plumbing
</div>
<div className="col-sm-2 workCatagory" onClick={() => setCatagory("other3")}>
test3
</div>
<div className="col-sm-2 workCatagory" onClick={() => setCatagory("other4")}>
Test4
</div>
And then I am setting the displayWork
to the value of the filtered array like so:
<div className="row workBars" onClick={() => handleClick()}>
const handleClick = () => {
setDisplayWork(filteredWork)
}
The problem with this approach is the change is not reflected immediately upon clicking the button. Using this solution here I was able to fix this
useEffect(() => {
handleClick()
}, [handleClick])
But I ran into an error message and the page crashing on me very often, I cannot get the state to update immediately without also creating an infinite loop. A link to my full page is here github I appreciate any advice you may have