I am creating an items listing with categories.
I want to have it such that when user first visits the site, the default first category should be active, but at the moment, all items are appearing.
After clicking on particular category then item results are right.
You can check full codesandbox here.
It's because for the
workItemsstate, you initialize it withuseState(workingList)which contains all the items. Meanwhile, the filtering based on thecategoryhappens only after aWorkListelement is clicked (which call thesetWorkItems).My advice is to put the
activestate in the root directlyApp.js, and remove theworkingItemsstate. Instead, you can make it directly as a variable that is calculated during render. Also, for things that don't change (likecategoriesstate here), you don't need to put it insideuseState.Codesandbox