I have the following useEffect()
:
useEffect(() => {
if (portfolios && portfolios.length > 0) {
const wLocation = portfolios.filter((portfolio: Portfolios) =>
portfolio.organizations.some(organization => organization.locations ?? false),
);
const wOutLocation = portfolios.filter((portfolio: Portfolios) =>
portfolio.organizations.every(organization => organization.locations ?? true),
);
setPortfoliosWOutLocation(getItemsWRGB(wOutLocation.map(portfolioFormatted)));
setPortfoliosWLocation(getItemsWRGB(wLocation.map(portfolioFormatted)));
}}, [portfolios]);
One of my partners tell me that I could use useMemo in order to avoid re calculation in every render, so I did this:
const portfolioWLocation = useMemo(() => {
const wLocation = portfolios.filter((portfolio: Portfolios) =>
portfolio.organizations.some(organization => organization.locations ?? false),
);
return getItemsWRGB(wLocation.map(portfolioFormatted));}, [portfolios]);
const portfolioWOutLocation = useMemo(() => {
const wOutLocation = portfolios.filter((portfolio: Portfolios) =>
portfolio.organizations.every(organization => organization.locations ?? true),
);
return getItemsWRGB(wOutLocation.map(portfolioFormatted));}, [portfolios]);
Will not be the same since portfolios
is the dependency for useMemo()
and useEffect()
.