For your information, I have following code:
const companyPagesAdapter = createEntityAdapter<CompanyPage>(
{
selectId: model => model.pageNumber,
sortComparer: (a, b) => a.pageNumber>b.pageNumber? 1 : a.pageNumber<b.pageNumber? -1 : 0
}
);
type CompanyPage = {
companyIds: number[],
pageNumber: number,
isDirty: boolean
}
So my state in companyPagesSlice slice looks like this:
{
ids: number[],
entities: {
[key: number]: CompanyPage;
}
}
Then in my component I extract company ids for a page like this:
const companyIds = useSelector(state => state.companyPages.entities[pageNumber].companyIds);
My companySlice slice state looks like this:
{
ids: number[],
entities: {
[key: number]: ICompany;
}
}
where ICompany is:
type ICompany = {
id: number;
name: string;
status: Status;
country: string;
city: string;
address: string;
zipCode: number;
}
Is there any possible way to select multiple entities from redux store with useSelector hook by passing an array of ids or anything else?
I tried to solve it this way:
const companies = companyIds?.map(item => {
const company = useSelector(state => selectCompanyById(state.companies, item));
if (!company)
return;
return company;
})
But it unfortunately breaks rule of react hooks: you can't evoke hook inside a loop.