So I am having an issue with filtering functionality using react hooks where I have a set of filters with tags associated to the filters. A user clicks on a filter and then the if the company has a tag associated with it then it will only show those filters.
Now when they click on a filter I need that filter to go into the array then from that array I want to do the company filtering and setCompanies - however I get an error as you cant setCompanies inside useEffect.
In this example there would be a button with tag node - would then need to filter all the companies that have node associated with them. If they unclick the filter then it needs to show all filters hence the filters.length === 0 piece of code.
How would you set this up properly so it does not error?
const company = [{name:'google', tags:['javascript', 'node']}, {name:'facebook', tags:['javascript', 'ruby']}]
function App() {
const [filters, setFilter] = useState([])
const [companies, setCompanies] = useState(company)
useEffect(() => {
filterCompany()
}, [filters, companies]);
function addOrRemoveFilter(item) {
if (filters.includes(item)) {
const filteredItems = filters.filter(filteredItem => filteredItem !== item)
setFilter([...filteredItems])
} else {
setFilter([...filters, item])
}
}
function filterCompany() {
const res = companies.filter(company => {
return filters.every(filter => {
return company.tags.includes(filter)
})
})
const listOfCompanies = filters.length === 0 ? company : res
setCompanies([...listOfCompanies])
}
return (
<div className="App">
<div className="columns is-multiline filters is-centered">
{data.values.map((item, index) => (
<HomeFilters key={index} data={item} onFilterClick={(item) => addOrRemoveFilter(item)} />
))}
</div>
<div className="columns is-multiline is-centered ">
{companies.map((item, index) => (
<Companies key={index} data={item} />
))}
</div>
</div>
);
}
export default App;