Answer the question
In order to leave comments, you need to log in
How can I optimize calculations in the react component?
Hey! There is a project on react + redux, on hooks the
Store looks something like this:
{
items: [...], // массив объектов с товарами, по типу { id: 1, title: 'hello', price: 100 }
activeItemId: 1, // ID выбранного итема из массива items
}
const activeItemId = useSelector((state) => state.activeItemId);
const items = useSelector((state) => state.items);
const activeItem = items.find((item) => item.id === activeItemId);
Answer the question
In order to leave comments, you need to log in
Usually items is made as an object, and then activeItem = items[activeItemId], this is very fast.
But if you need an array, then you can do this:
const activeItem = useMemo(() => items.find((item) => item.id === activeItemId), [items, activeItemId]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question